有时候我们希望在sql一个表里面的column, 一部分被分化成另一个class
典型的例子是 Address
直接看代码:
[Table("member")] public class Member { [Key] public Int32 memberId { get; set; } public string name { get; set; } public virtual Address address { get; set; } } /* 不能公开 - public DbSet<Address> addresss { get; set; } <-don‘t do this */ [ComplexType] //写一个标签 public class Address { /* 这里一定要写 column 即使名字一样,因为defaul的命名法是 Address_postcode */ [Column("postcode")] public string postcode { get; set; } [Column("country")] public string country { get; set; } }
insert
db.members.Add(new Member { name = "lu xiao feng", address = new Address { country = "MALAYSIA", postcode = "81300" } }); db.SaveChanges();
时间: 2024-09-30 07:37:41