实体框架两端可选1对1关系 [英] Entity framework optional 1 to 1 relation on both ends

查看:93
本文介绍了实体框架两端可选1对1关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到的大多数问题都不是我正在寻找的类型。



我有2个表:

  public class User 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserId {get;组; }

public Guid? CustomerId {get;组; }

[ForeignKey(CustomerId)]
public Customer Customer {get;组;


public class Customer
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid CustomerId {get;组; }

public Guid? UserId {get;组; }

[ForeignKey(UserId)]
public User User {get;组;
}

A 用户可以不存在客户客户可以存在而不是用户



我尝试了流畅的API,如下所示:

  modelBuilder.Entity< Customer>()
.HasOptional< User>(c => c.User)
.WithOptionalDependent(c => c.Customer)
.Map c => c.MapKey(UserId));

但是当我尝试创建迁移时,它会不断给我这个错误:



Customer_User_Source::在Customer_User关系中的角色Customer_User_Source中,多项式无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须为*。



更新



我将模型更改为:

  public class User 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserId {get;组; }

public virtual Customer Customer {get;组;


public class Customer
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid CustomerId {get;组; }

public virtual User User {get;组;
}

使用流畅的API:

  modelBuilder 
.Entity< Customer>()
.HasOptional(l => l.User)
.WithOptionalPrincipal()
.Map(k => k.MapKey(CustomerId));

modelBuilder
.Entity< User>()
.HasOptional(c => c.Customer)
.WithOptionalPrincipal()
.Map k => k.MapKey(UserId));

这似乎有效,但是有一种方法来定义模型中的列,而不必使用 MapKey

解决方案

请参阅链接2 也是



该链接的问题是我不确定是否提供了一个解决问题的实际解决方案:因为我不知道它是否提供了两个表中的外键的唯一性(因为这个链接建议)。因为没有EF唯一约束,你必须手动创建它(在生成器中)



最后链接解释说,执行1:*关系的独特形式是使用其中一个的外键表格作为另一个的主键。



祝你好运。


Most questions I have found were not the type I am looking for.

I have 2 tables:

public class User
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserId { get; set; }

    public Guid? CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public Customer Customer { get; set; }
}

public class Customer
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid CustomerId { get; set; }

    public Guid? UserId { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }
}

A User can exists without being a Customer and a Customer can exist without being an User.

I tried the fluent API like this:

modelBuilder.Entity<Customer>()
    .HasOptional<User>(c => c.User)
    .WithOptionalDependent(c => c.Customer)
    .Map(c => c.MapKey("UserId"));

But it keeps giving me this error when I try to create the migration:

Customer_User_Source: : Multiplicity is not valid in Role 'Customer_User_Source' in relationship 'Customer_User'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

Update

I changed my model to this:

public class User
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserId { get; set; }

    public virtual Customer Customer { get; set; }
}

public class Customer
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid CustomerId { get; set; }

    public virtual User User { get; set; }
}

With the fluent API:

modelBuilder
    .Entity<Customer>()
    .HasOptional(l => l.User)
    .WithOptionalPrincipal()
    .Map(k => k.MapKey("CustomerId"));

modelBuilder
    .Entity<User>()
    .HasOptional(c => c.Customer)
    .WithOptionalPrincipal()
    .Map(k => k.MapKey("UserId"));

This seems to work but is there a way to define the column in the model instead of having to use MapKey?

解决方案

See link 1

And link 2 too

The problem with that links is I'm not sure if they provide a actual solution to the problem: because I don't know if it provide uniqueness for the foreign key in both tables (as this link suggest). Because that, not having EF unique constraints, you have to create it manually (in the generator)

And finally link explains that the unique form of performing an 1:* relationship is using the foreign key of one of the tables as primary key of the other.

Good luck.

这篇关于实体框架两端可选1对1关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆