Emi*_*len 10 c# asp.net-mvc entity-framework
基本上我有一个像这样的实体:
public class Person {
public int PersonId { get; set; }
public string Name { get; set; }
public Address Hometown { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有一个类:
public class Address {
public City City { get; set; }
public string Province { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想要完成的是垂直连接两个类并有一个表行:
TB_PERSON:
PersonId PK
Name
City_id FK
Province
Run Code Online (Sandbox Code Playgroud)
为什么我想要这种方法,在我的实际项目中,我在多个条目上发生了相同类型的数据结构模式,在这种情况下,示例将是地址类.它可能很容易出现在另一个实体中.
难道我几天都找不到怎么做这么难吗?我能得到的是最复杂的类型,但在这种情况下它们不允许导航属性.我希望访问并使我的行数据类型化,面向对象,以为EF会有所作为.任何帮助表示赞赏.
ComplexType 应该是一个解决方案,但不幸的是:
复杂类型不能包含导航属性。 来源
解决方法列表:
表拆分的解决方法
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public Int32 ID { get; set; }
public string Province { get; set; }
public virtual City City { get; set; }
}
public class City
{
public Int32 CityID { get; set; }
public string Name { get; set; }
}
public class MappingContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Address> Addresses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Address>()
.HasKey(t => t.ID)
.HasOptional(t => t.City)
.WithMany()
.Map(t => t.MapKey("CityID"));
modelBuilder.Entity<Address>()
.Property(t => t.ID)
.HasColumnName("PersonID");
modelBuilder.Entity<Person>()
.HasKey(t => t.PersonID)
.HasRequired(t => t.Address)
.WithRequiredPrincipal();
modelBuilder.Entity<Person>().ToTable("TB_PERSON");
modelBuilder.Entity<Address>().ToTable("TB_PERSON");
modelBuilder.Entity<City>()
.HasKey(t => t.CityID)
.ToTable("City");
}
}
Run Code Online (Sandbox Code Playgroud)
[用法]
using (var db = new MappingContext())
{
var person = db.Persons.FirstOrDefault();
var cityName = person.Address.City.Name;
var address = db.Addresses.FirstOrDefault();
var personName = address.Person.Name;
}
Run Code Online (Sandbox Code Playgroud)
[数据库]
CREATE TABLE [dbo].[City](
[CityID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[TB_PERSON](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Province] [varchar](50) NULL,
[CityID] [int] NULL
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)
表拆分 + TPC 继承的解决方法(用于可重用的 Address 类)
TB_CUSTOMER 是另一个包含地址列的表。
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual PersonAddress Address { get; set; }
}
public class Address
{
public string Province { get; set; }
public virtual City City { get; set; }
}
public class PersonAddress : Address
{
public Int32 PersonID { get; set; }
public virtual Person Person { get; set; }
}
public class CustomerAddress : Address
{
public Int32 CustomerID { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public virtual CustomerAddress Address { get; set; }
}
public class City
{
public Int32 CityID { get; set; }
public string Name { get; set; }
}
public class MappingContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<PersonAddress> PersonAddresses { get; set; }
public DbSet<CustomerAddress> CustomerAddresses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonAddress>()
.HasKey(t => t.PersonID)
.HasOptional(t => t.City)
.WithMany()
.Map(t => t.MapKey("CityID"));
modelBuilder.Entity<CustomerAddress>()
.HasKey(t => t.CustomerID)
.HasOptional(t => t.City)
.WithMany()
.Map(t => t.MapKey("CityID"));
modelBuilder.Entity<Person>()
.HasRequired(t => t.Address)
.WithRequiredPrincipal(t => t.Person);
modelBuilder.Entity<Customer>()
.HasRequired(t => t.Address)
.WithRequiredPrincipal();
modelBuilder.Entity<Person>().ToTable("TB_PERSON");
modelBuilder.Entity<PersonAddress>().ToTable("TB_PERSON");
modelBuilder.Entity<Customer>().ToTable("TB_CUSTOMER");
modelBuilder.Entity<CustomerAddress>().ToTable("TB_CUSTOMER");
modelBuilder.Entity<City>()
.HasKey(t => t.CityID)
.ToTable("City");
}
}
Run Code Online (Sandbox Code Playgroud)
IAddress 的解决方法
public class Person : IAddress
{
public int PersonID { get; set; }
public string Name { get; set; }
public string Province { get; set; }
public virtual City City { get; set; }
[NotMapped]
public IAddress Address { get { return this; } }
}
public interface IAddress
{
string Province { get; set; }
City City { get; set; }
}
public class City
{
public Int32 CityID { get; set; }
public string Name { get; set; }
}
public class MappingContext : DbContext
{
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasKey(t => t.PersonID)
.HasOptional(t => t.City)
.WithMany()
.Map(t => t.MapKey("CityID"));
modelBuilder.Entity<Person>().ToTable("TB_PERSON");
modelBuilder.Entity<City>()
.HasKey(t => t.CityID)
.ToTable("City");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
260 次 |
| 最近记录: |