2014-11-22声明方式
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int SKU { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public virtual string ImageURL { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Product>()
.Map(m =>
{
m.Properties(p => new {p.SKU, p.Price, p.Description});
m.ToTable("Table1");
}).Map(m =>
{
m.Properties(p => new {p.SKU, p.ImageURL});
m.ToTable("Table2");
});
}
}
怎么使用
static void Main(string[] args)
{
using (var context=new ProductContext())
{
var products = new List<Product>
{
new Product {SKU = , Price = 12.1m, Description = "1test", ImageURL = "1.jpg"},
new Product {SKU = , Price = 12.2m, Description = "2test", ImageURL = "2.jpg"},
new Product {SKU = , Price = 12.3m, Description = "3test", ImageURL = "3.jpg"},
new Product {SKU = , Price = 12.4m, Description = "4test", ImageURL = "4.jpg"},
new Product {SKU = , Price = 12.5m, Description = "5test", ImageURL = "5.jpg"},
new Product {SKU = , Price = 12.6m, Description = "6test", ImageURL = "6.jpg"}
};
context.Products.AddRange(products);
context.SaveChanges();
}
using (var context = new ProductContext())
{
foreach (var product in context.Products)
{
Console.WriteLine("{0}--{1}--{2}--{3}", product.SKU, product.Price, product.Description,
product.ImageURL);
}
Console.ReadKey();
}
}
生成表结构

运行效果
