處理狀況

當要同一時間新增大量資料時,如果單純用EF的新增方式,速度會讓人吐血。 所以需要透過BulkInsert的方式處理,但是又不想自己另外寫ado的方式處理,所以就要透過extenstion的方式, 讓EF的功能加強一下

##安裝 EntityFramework.BulkInsert-ef6

Nuget

##參考文件

http://efbulkinsert.codeplex.com/

##(用法)Demo Code

1
2
3
4
5
6
7
8
using(context db = new context()){
// 原本會用
// db.tables.AddRange(entities);
// 改用
db.BulkInsert(entities);

db.SaveChanges();
}

##執行結果 非常快速

Ref: http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html

如何動態產生Database

如果利用Code first的方式,搭配DbMigration即可完成工作

  • DBContext.cs
1
2
3
4
5
6
7
8
9
10
11
public class DemoContext : DbContext
{
public DemoContext() { }
public DemoContext(string ConnectionString)
: base(ConnectionString)
{
Database.SetInitializer(new CustomInitializer());
Database.Initialize(true);
}
public DbSet<Blog> Blogs { get; set; }
}
  • CustomInitializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<DemoContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
MigrationsDirectory = @"Migration";
}


protected override void Seed(DemoContext context)
{
}
}

class CustomInitializer : IDatabaseInitializer<DemoContext>
{
public void InitializeDatabase(DemoContext context)
{
if (!context.Database.Exists() || !context.Database.CompatibleWithModel(false))
{
var configuration = new Configuration();
configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient");
var migrator = new DbMigrator(configuration);
migrator.Update();
}
}
}
  • client端
1
2
using (var db = new AtaBookContext(connection1)){}
using (var db = new AtaBookContext(connection2)){}

connection1和connection2分別指到不同的資料庫,EF就會根據連線字串的設定產生相對應的資料庫

當要在htmlHelper裡面使用angularjs的屬性時, 原本的ng-model變成ng_model, 將 - 變成 _ 即可

例如:

1
2
3
4
5
6
@Html.DropDownList("dropdown", (IEnumerable<SelectListItem>)ViewBag.items, new { ng_model = "currentSelect", ng_change = "selectChanges()" })

會產生
<select id="dropdown" name="dropdown" ng-change="selectChanges()" ng-model="currentSelect">
<options>something</options>*n
</select>

#NOTE: Youtube link: http://youtu.be/S8XL1L_1Lyw

  • Abstract Factory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{
public static Object Create(string className, Dictionary<String, Object> values)
{
Type type = Type.GetType(className);
Object instance = Activator.CreateInstance(type);
foreach (var entry in values)
{
type.GetProperty(entry.Key).SetValue(instance, entry.Value, null);
}
return instance;
}
static void Main(string[] args)
{
Console.WriteLine(Create("Patterns.Book", new Dictionary<string, object>()
{
{"Title", "Some titles"},
{"Pages", 100}
}));

Console.WriteLine(Create("Patterns.CD", new Dictionary<string, object>()
{
{"Title", "Some CD"},
{"Volume", 12}
}));
}
}

class Book
{
public string Title { get; set; }
public int Pages { get; set; }
public override string ToString()
{
return string.Format("Book {0} {1}", Title, Pages);
}
}

class CD
{
public string Title { get; set; }
public int Volume { get; set; }
public override string ToString()
{
return string.Format("CD {0} {1}", Title, Volume);
}
}
}

  • Cascade Pattern
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{

static void Main(string[] args)
{
// Sect I
//Mailer mailer = new Mailer();
//mailer.from("[email protected]");
//mailer.to("[email protected]");
//mailer.subject("subjeccccttt");
//mailer.message("contents blah blah");
//mailer.send();

// Sect II- Cascade way
new Mailer().from("[email protected]")
.to("[email protected]")
.subject("subjeccccttt")
.message("contents blah blah")
.send();
}
}
// Sect I
//class Mailer
//{
// public void to(string toAdrr) { }
// public void from(string fromAddr) { }
// public void subject(string sub){}
// public void message(string msg) { }
// public void send() { }
//}

// Sect II
class Mailer
{
public Mailer to(string toAdrr) { return this; }
public Mailer from(string fromAddr) { return this; }
public Mailer subject(string sub) { return this; }
public Mailer message(string msg) { return this; }
public void send() { }
}
}

  • Pluggable Behavior
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{
static int totalValues(int[] values)
{
int total = 0;
foreach (var value in values)
{
total += value;
}
return total;
}
static int totalEvenValues(int[] values)
{
int total = 0;
foreach (var value in values)
{
if (value % 2 == 0) total += value;
}
return total;
}
static int totalOddValues(int[] values)
{
int total = 0;
foreach (var value in values)
{
if (value % 2 != 0) total += value;
}
return total;
}
static int totalSelectValues(int[] values, Func<int, bool> selector)
{
int total = 0;
foreach (var value in values)
{
// pass value(first param) into function and return second param value
if (selector(value)) total += value;
}
return total;
}
//
static void Main(string[] args)
{
int[] values = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Console.WriteLine(totalValues(values));
Console.WriteLine(totalEvenValues(values));
Console.WriteLine(totalOddValues(values));

Console.WriteLine("==================");
// re-write
// define functioin in lambda format
// ref: http://msdn.microsoft.com/zh-tw/library/bb397687(v=vs.110).aspx
Console.WriteLine(totalSelectValues(values, (value) => true));
Console.WriteLine(totalSelectValues(values, (value) => value % 2 == 0));
Console.WriteLine(totalSelectValues(values, (value) => value % 2 != 0));
}
}
}

  • Execute Around Method Pattern

part1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{
class Resource
{
public Resource()
{
Console.WriteLine("Creating...");
}
public void op1()
{
Console.WriteLine("op1...");
}
public void op2()
{
Console.WriteLine("op2...");
}
~Resource()
{
Console.WriteLine("cleanup exensive resource");
}

}
public static void Main(string[] args)
{
{
Resource resource = new Resource();
resource.op1();
resource.op2();
}
Console.WriteLine("out of the block");
}
}
}

執行結果: 執行結果

part2 (with using())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{
class Resource : IDisposable
{
public Resource()
{
Console.WriteLine("Creating...");
}
public void op1()
{
Console.WriteLine("op1...");
}
public void op2()
{
Console.WriteLine("op2...");
}
~Resource()
{
Clearnup();
}
public void Dispose()
{
Clearnup();
GC.SuppressFinalize(this);
}
private void Clearnup()
{
Console.WriteLine("cleanup exensive resource");
}
}
public static void Main(string[] args)
{
// this require class that implement IDisposable
using (Resource resource = new Resource())
{
resource.op1();
resource.op2();
}
Console.WriteLine("out of the block");
}
}
}

執行結果

part3 with Execute Around Method Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Patterns
{
class Program
{
class Resource
{
//Call Resource class from here
public static void Use(Action<Resource> block)
{
Resource resouce = new Resource();
try
{
block(resouce);
}
finally
{
resouce.Clearnup();
}

}

//change public to protected
// can't new class in normal way : class _class = new class()
protected Resource()
{
Console.WriteLine("Creating...");
}

public void op1()
{
Console.WriteLine("op1...");
}
public void op2()
{
Console.WriteLine("op2...");
}

private void Clearnup()
{
Console.WriteLine("cleanup exensive resource");
}
}
public static void Main(string[] args)
{
{
Resource.Use(
(resource) =>
{
resource.op1();
resource.op2();
});
}
Console.WriteLine("out of the block");
}
}
}

執行結果

Ref from G+

Ater upgrading to AS 0.5.4 I am getting this error 「Failed to set up SDK Error:Module 『AndroinoTerm』: platform 『android-18』 not found」 while I already have android-18 installed. My sdk path is correctly setup and I am also able to run my project successfully. Does Anybody else having this issue ?

Solution: 1. Change sdk folder location 2. update sdk path in android studio 3. change back to original location 4. update sdk path setting in android studio again 5. problem solved

當網頁要讀取一個很大量的資料時,通常都會透過分頁的方式來顯示資料。 如果透過WebApi+OData的特性來做分頁,作法很單純 設定 WebApiConfig.cs

1
2
//加入
config.EnableQuerySupport();

假設原本的API寫法如下

1
2
3
4
5
[Route("api/Customer")]
public IEnumerable<customers> Getcustomers()
{
return db.customers.AsEnumerable();
}

改成

1
2
3
4
5
[Route("api/Customer")]        
public IQueryable<customers> Getcustomers()
{
return db.customers.AsQueryable();
}

那在client端要呼叫api的url中,在加上OData的查詢語法來取得所要的資料區段,來達成分頁的效果

指令 說明 範例
top 結果挑出最前面的幾筆 ?$top=3
skip 略過幾筆。可用於分頁顯示 ?$skip=10
orderby 排序 ?$orderby=SupplierID,ProductID
filter 篩選
gt : > , 大於 $filter=ProductID gt 10
lt : < , 小於 $filter=ProductID lt 10
ge : >=, 大於等於 $filter=ProductID ge 10
le : <=, 小於等於 $filter=ProductID le 10
eq : =, 等於 $filter=ProductID eq 10
ne : <>, 不等於 $filter=ProductID ne 10

參考資料: http://msdn.microsoft.com/en-us/library/windowsazure/gg312156.aspx

如果要利用OData取得所有的資料筆數時,上述的寫法沒有辦法做到,所以要稍微改寫一下原本的寫法

1
2
3
4
5
6
7
8
9
[Route("api/Customer")]   
public PageResult<customers> Getcustomers(ODataQueryOptions<customers> options)
{
var results = options.ApplyTo(db.customers.AsQueryable());
return new PageResult<customers>(
results as IEnumerable<customers>, // Items
Request.GetNextPageLink(), // NextPageLink
Request.GetInlineCount()); // Count
}

要傳入的東西是一樣的,但是回傳的結果會變成

1
2
3
4
5
{
"Items":[data....],
"NextPageLink": null,
"Count": 12345
}

所以在接收時要再調整

當$filter的查詢欄位是guid時

所送出的值前面要加上 guid 的關鍵字

1
?$filter=field eq guid'<value>'

  1. In your Windows 8 VM, go to Control Panel > System > Advanced system settings > Computer Name and click Change. Name this whatever you like, e.g. 「windows」. Restart your VM.

  2. Open CMD or Powershell as administrator. Add a URL ACL entry for your new name on the port of your choice, e.g. netsh http add urlacl url=http://windows:8080/ user=everyone

  3. Open CMD or Powershell as administrator. Add an inbound firewall rule for this new port. In Windows 8, the syntax is: netsh advfirewall firewall add rule name=「IISExpressWeb」 dir=in action=allow protocol=TCP localport=8080

In Windows 7, the syntax is: netsh firewall add portopening TCP 8080 IISExpressWeb enable ALL

  1. Edit your IISExpress applicationhost.config file, typically found at your Documents\IISExpress\config\applicationhost.config. Find your site under sites, and add a binding to the port using your machine name, e.g.

  2. Startup IISExpress with Visual Studio, hit your URL from a browser on your Mac/VM Host, e.g. http://windows:8080

  3. Revel in your magnificence.

More info here: http://stackoverflow.com/questions/3313616/iis-express-enable-external-request http://stackoverflow.com/questions/5442551/iisexpress-returns-a-503-error-from-remote-machines http://www.hanselman.com/blog/WorkingWithSSLAtDevelopmentTimeIsEasierWithIISExpress.aspx

Ref: https://gist.github.com/justingarrick/6322779

1.在xsd裡描述minOccurs="0"時 , 所建立的xml裡面要手動控制是否輸出value

c#可以用class轉換成xml, 但是在class裡面的property並不是每一個都需要輸出,有些事非必要性的欄位。 這需要在property欄位另外加上一個欄位來做設定

1
2
3
4
5
6
7
8
class demo{
public string field1;
public string field2;
public customEnum field3;
// 以下這行是關鍵
[XmlIgnore]
public bool field3Specified;
}

語法是 propertyNameSpecified

[XmlIgnore] : 功用是在輸出xml時,忽略此欄位

依上列的案例來說,如果 field3Specified=true, 則輸出field3欄位, 如果設定成false, 則不輸出field3

2.使用xsd.exe將.xsd檔案轉換成class

在visual studio tools下-> 命令視窗 -> xsd

ref: http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx

將.xsd檔案轉換成class的方式為

1
xsd file.xsd /c

資本公積

資本公積是指由股東投入、但不能構成股本或實收資本的資金部分,主要包括股本溢價、接受捐贈實物資產、投入資本匯兌損益、法定財產重估增值以及投資準備金等。

資本公積的來源,按其用途主要包括兩類: 一、超過票面金額發行股票所得之溢額。

  1. 以超過面額發行普通股或特別股溢價:無。
  2. 公司因企業合併而發行股票取得他公司股權或資產淨值所產生股本溢價:無。惟,合併基準日在97.12.31前案件,合併股本溢價來源屬消滅公司帳載未分配盈餘部分,如用以配發股票或現金給股東,仍屬股東股利所得。
  3. 庫藏股票交易溢價:有。
  4. 轉換公司債相關應付利息補償金於約定賣回期間屆滿日可換得普通股市價高於約定賣回價格時轉列金額:無。
  5. 因認股權證行使所得股本發行價格超過面額部分:無。
  6. 特別股或公司債轉換為普通股,原發行價格或帳面價值大於所轉換普通股面額差額:無。
  7. 附認股權公司債行使普通股認股權證分攤價值:無。
  8. 特別股收回價格低於發行價格差額:可能有。惟廣義而言,特別股的發行價格高於收回價格部分,也是股東的出資額,可否不視為股東有所得?
  9. 認股權證逾期未行使而將其帳面餘額轉列者:有。
  10. 因股東逾期未繳足股款而沒收已繳股款:可能有。惟廣義而言,沒收股款也是股東的出資額,可否不視為股東有所得?
  11. 公司因企業分割而發行股票取得他公司營業或資產淨值所產生股本溢價:無。
  12. 公司因股份轉換而發行股票取得他公司股份或股權所產生股本溢價:可能有。依財政部91.8.19台財稅第0910454466號涵意旨,股本溢價來源屬他公司轉換前帳載未分配餘者,如用以分配股票或現金給股東,屬股東股利所得,惟此函釋合理性,有待斟酌。

二、受領贈與之所得。

  1. 受領股東贈與本公司已發行股票:可能有。但廣義而言,此部分也是股東的出資額,可否不視為股東有所得?
  2. 股東依股權比例放棄債權或依股權比例捐贈資產:可能無。

資本公積的主要用途有兩個,

  1. 轉增資本
  2. 彌補虧損。

盈餘公積(公司法:237條)

公司於完納一切稅捐後,分派盈餘時,應先提出百分之十為法定盈餘公積。但法定盈餘公積,已達資本總額時,不在此限。 除前項法定盈餘公積外,公司得以章程訂定或股東會議決,另提特別盈餘公積。 公司負責人違反第一項規定,不提法定盈餘公積時,各科新臺幣六萬元以下罰金。

惟依據銀行法第50條規定,銀行於完納一切稅捐後分派盈餘時,應先提30%為法定盈餘公積,另證券金融公司亦比照銀行提列30%法定盈餘公積,此為較特殊處。

種類

  1. 法定盈餘公積: 指依公司法或其他相關法令規定自盈餘中指撥之公積
  2. 特別盈餘公積: 因特定目的(如平衡盈餘分派、擴充改良設備或作為償債準備)依公司章程或股東會決議提存之公積。

用途 (公司法239條第1項暨232條第2項、241條第3項規定)

  • 彌補虧損
  • 分配(僅限法定盈餘公積)
  1. 為於公司無盈餘時,以法定盈餘公積超過實收資本額50%之部分派充股息及紅利
  2. 為法定盈餘公積已達實收資本50%者,得保留法定盈餘公積達實收資本50%之半數,其餘部分得以撥充資本,而股東依原有股份比例發給新股。

###其他備註

  • 補累積虧損之順序,宜先動用法定盈餘公積,次為特別盈餘公積,最後為資本公積。
  • 特別盈餘公積得用於彌補虧損,惟於原提列特別盈餘公積之原因消除前,如曾以特別盈餘公積彌補虧損者,於未來有盈餘之年度,應先就特別盈餘公積不足數額補足提列,始得分派盈餘