简体   繁体   English

EF一对一映射

[英]EF mapping one to one optional

I have som mapping problem with EF. 我有EF的som映射问题。

This is my classes 这是我的课

public class User{
    public Guid Id { get; set; }
    // Fullname of the user account owner
    public string Name { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public Player Player { get; set; }
}

public class Player
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual User User { get; set; }
}

It works fine, but now I want to create the navigation property Player and User in this classes. 它工作正常,但现在我想在此类中创建导航属性Player和User。 I have this Fluent code: 我有这个流利的代码:

        modelBuilder.Entity<User>()
            .HasOptional(x => x.Player)
            .WithOptionalDependent(x => x.User)
            .Map(x => x.MapKey("Username"));

But I only get this error message, and I have no ide what's wrong. 但是我只会收到此错误消息,而且我不知道这是怎么回事。

Each property name in a type must be unique. 类型中的每个属性名称都必须是唯一的。 Property name 'Username' was already defined. 属性名称“用户名”已定义。

My DB setup looks like the classes, in the player table the Name is unique. 我的数据库设置看起来像类,在播放器表中,名称是唯一的。 It's not unique in the User table. 在用户表中不是唯一的。 A user can exist without a player and vice versa. 用户可以不存在玩家而存在,反之亦然。 (Actully I don't want any User property inside the Player class but I think it's a requierment?!) (实际上,我不需要Player类内的任何User属性,但我认为这是必要条件吗?!)

I think it's complaining about the fact that UserName is already a property in the object model. 我认为这是在抱怨UserName已经是对象模型中的一个属性。 See the docs for the Map() method: 请参阅有关Map()方法的文档:

From http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.configuration.foreignkeynavigationpropertyconfiguration.map%28v=vs.103%29 : 来自http://msdn.microsoft.com/zh-cn/library/system.data.entity.modelconfiguration.configuration.foreignkeynavigationpropertyconfiguration.map%28v=vs.103%29

Configures the relationship to use foreign key property(s) that are not exposed in the object model. 将关系配置为使用未在对象模型中公开的外键属性 The column(s) and table can be customized by specifying a configuration action. 可以通过指定配置操作来自定义列和表。 If an empty configuration action is specified then column name(s) will be generated by convention. 如果指定了空的配置操作,则将按惯例生成列名。 If foreign key properties are exposed in the object model then use the HasForeignKey method. 如果外键属性在对象模型中公开,则使用HasForeignKey方法。 Not all relationships support exposing foreign key properties in the object model. 并非所有关系都支持在对象模型中公开外键属性。

Remove the modelBuilder code and mark the PrimaryKey as a ForeignKey on the dependent table. 删除modelBuilder代码,并将主键标记为从属表上的外键。 For example if Players don't exist without a User: 例如,如果没有用户就不存在玩家:

public class User
{
    public Guid Id { get; set; }
    // Fullname of the user account owner
    public string Name { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }

    public Player Player { get; set; }
}

public class Player
{
    [ForeignKey("User")]
    public Guid Id { get; set; }

    public string Name { get; set; }

    public virtual User User { get; set; }
}

The ForeignKey attribute tells EF which side of the one-to-one is dependent, allowing it to map it properly. ForeignKey属性告诉EF一对一的哪一侧是从属的,从而允许它正确地映射它。

If your columns in the database has the same name as the properties of your model you don't need to map the property ".Map(x => x.MapKey("Username"));" 如果数据库中的列与模型的属性具有相同的名称,则不需要映射属性“ .Map(x => x.MapKey(” Username“));” EF already mapped the property "Username" using the convention and is because of that the EF is complaining EF已经使用约定映射了属性“ Username”,原因是EF抱怨

With your entities 与您的实体

...I just like to do it the other way around: ...我只是想反过来​​做:

modelBuilder.Entity<Player>()
    .HasRequired(i => i.User)
    .WithRequiredDependent(i => i.Player);

or this (optional): 或此(可选):

modelBuilder.Entity<Player>()
    .HasRequired(i => i.User)
    .WithOptional(x => x.Player);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM