EF代码第一 - 1对1可选关系 [英] EF Code First - 1-to-1 Optional Relationship

查看:200
本文介绍了EF代码第一 - 1对1可选关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



简单模式:


我想在现有数据库中使用EF Code First映射一个可选的1对1关系。

 用户
用户名
联系人

联系
ID
名称

显然,ContactID加入Contact.ID。 ContactID字段为空,所以关系是可选的 - 0或1,从不会很多。



那么如何在EF Code First中用这个现有的模式指定这种关系?



以下是我迄今为止所尝试的:

  public class用户
{
[Key]
public string Username {get;组; }
public int? ContactID {get;组; }

[ForeignKey(ContactID)]
public virtual联系Contact {get;组;
}

public class联系
{
[Key]
public int ID {get;组; }
public string Name {get;组; }

public virtual User User {get;组;
}

modelBuilder.Entity< User>()。HasOptional< Contact>(u => u.Contact)
.WithOptionalDependent(c => c.User );

我得到以下异常:

  System.Data.Edm.EdmAssociationEnd::在User_Contact关系中的角色
'User_Contact_Source'中,多项式无效。因为Dependent
角色属性不是关键属性,因此从属角色的多重性
的上限必须为*。


解决方案

一个解决方案是;

  public class User 
{
[Key]
public string Username {get;组; }

public virtual联系Contact {get;组;
}

public class联系
{
[Key]
public int ID {get;组; }
public string Name {get;组; }

public virtual User User {get;组;
}

modelBuilder.Entity< User>()
.HasOptional< Contact>(u => u.Contact)
.WithOptionalDependent(c => ; c.User).Map(p => p.MapKey(ContactID));

您只在POCO中设置导航对象,而是使用流畅的API将您的密钥映射到正确栏。


I want to map an optional 1-to-1 relationship in an existing database with EF Code First.

Simple schema:

User
 Username
 ContactID

Contact
 ID
 Name

Obviously ContactID joins to Contact.ID. The ContactID field is nullable so the relationship is optional - 0 or 1, never many.

So how do I specify this relationship in EF Code First, with this existing schema?

Here's what I've tried so far:

public class User
{
    [Key]
    public string Username { get; set; }
    public int? ContactID { get; set; }

    [ForeignKey("ContactID")]
    public virtual Contact Contact { get; set; }
}

public class Contact
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual User User { get; set; }
}

modelBuilder.Entity<User>().HasOptional<Contact>(u=> u.Contact)
    .WithOptionalDependent(c => c.User);

I get the following Exception:

  System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role
 'User_Contact_Source' in relationship 'User_Contact'. Because the Dependent 
Role properties are not the key properties, the upper bound of the multiplicity 
of the Dependent Role must be *.

解决方案

One solution would be;

public class User
{
    [Key]
    public string Username { get; set; }

    public virtual Contact Contact { get; set; }
}

public class Contact
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual User User { get; set; }
}

        modelBuilder.Entity<User>()
            .HasOptional<Contact>(u => u.Contact)
            .WithOptionalDependent(c => c.User).Map(p => p.MapKey("ContactID"));

You set only your navigational objects in your POCOs and instead you use fluent API to map your key to the correct column.

这篇关于EF代码第一 - 1对1可选关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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