[英]Entity Framework Fluent API Mapping Issue
How can I configure relationships between three tables to get all related data? 如何配置三个表之间的关系以获取所有相关数据?
I have the following models (and the same tables in database): 我有以下模型(和数据库中的相同表):
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public virtual ClientCard ClientCard { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
public virtual ClientCard ClientCard { get; set; }
}
public class ClientCard
{
public Guid ClientCardId { get; set; }
public Guid CardId { get; set; }
public Guid ClientId { get; set; }
public virtual Client Client { get; set; }
public virtual Card Card { get; set; }
}
And the following OnModelCreating method: 以及以下OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<ClientCard>()
.HasKey(x => x.ClientCardId);
builder.Entity<ClientCard>()
.HasRequired(x => x.Client)
.WithRequiredPrincipal(x => x.ClientCard);
builder.Entity<ClientCard>()
.HasRequired(x => x.Card)
.WithRequiredPrincipal(x => x.ClientCard);
base.OnModelCreating(builder);
}
But the results return without related data. 但是结果返回时没有相关数据。 Why?
为什么?
For your code please try something like: 对于您的代码,请尝试以下操作:
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public virtual ICollection<ClientCard> ClientCard { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
public virtual ICollection<ClientCard> ClientCard { get; set; }
}
public class ClientCard
{
public Guid ClientCardId { get; set; }
public Guid CardId { get; set; }
public Guid ClientId { get; set; }
public virtual Client Client { get; set; }
public virtual Card Card { get; set; }
}
And mapping: 并映射:
builder.Entity<Client>()
.HasKey(t => t.ClinetID);
....
builder.Entity<Client>()
.HasMany(a => a.ClientCard)
.WithRequired(p => p.Client)
.HasForeignKey(p => p.ClientID);
For Card 对于卡
....
builder.Entity<Card>()
.HasMany(a => a.ClientCard)
.WithRequired(p => p.Card)
.HasForeignKey(p => p.CardID);
But I think the best way is to create a separate map-file for each entities 但我认为最好的方法是为每个实体创建一个单独的映射文件
Now I am using the following models: 现在,我使用以下模型:
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public Guid? CardId { get; set; }
public virtual Card Card { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
//public virtual Client Client { get; set; }
}
and the following OnModelCreating method: 和以下OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<Client>()
.HasOptional(x => x.Card)
//.WithOptionalPrincipal(x=> x.Client)
//or
//.WithOptionalDependent(x=> x.Client)
;
base.OnModelCreating(builder);
}
All clients return with related data(if exists). 所有客户均返回相关数据(如果存在)。 But if I want to get ralated data with Card object and uncomment
public virtual Client Client { get; set; }
但是,如果我想使用Card对象获取相关数据并且取消注释
public virtual Client Client { get; set; }
public virtual Client Client { get; set; }
public virtual Client Client { get; set; }
and WithOptionalPrincipal/WithOptionalDependent I always get on of these SqlException: "invalid column name 'Client_ClientId'" (if WithOptionalPrincipal is used) OR "invalid column name 'Card_CardId'" (if WithOptionalDependent is used) public virtual Client Client { get; set; }
和WithOptionalPrincipal / WithOptionalDependent,我总是了解这些SqlException:“无效的列名'Client_ClientId'”(如果使用了WithOptionalPrincipal)或“无效的列名'Card_CardId'”(如果使用了WithOptionalDependent)
I finally found a solution for my case. 我终于找到了解决方案。
First, I have removed CardId from Client class. 首先,我从Client类中删除了CardId。 Second, I have changed my OnModelCreating method:
其次,我更改了OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<Client>()
.HasOptional(x => x.Card)
.WithOptionalDependent(x => x.Client)
.Map(x => x.MapKey("CardId"))
.WillCascadeOnDelete(false);
builder.Entity<Card>()
.HasOptional(x => x.Client)
.WithOptionalPrincipal(x => x.Card)
.WillCascadeOnDelete(false);
base.OnModelCreating(builder);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.