简体   繁体   中英

EF mapping one to one optional

I have som mapping problem with EF.

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. 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?!)

I think it's complaining about the fact that UserName is already a property in the object model. See the docs for the Map() method:

From http://msdn.microsoft.com/en-us/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. 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. 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.

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"));" EF already mapped the property "Username" using the convention and is because of that the EF is complaining

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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