c# - EF映射一对一可选

标签 c# mysql entity-framework entity-framework-4.1

我有 EF 的 som 映射问题。

这是我的类(class)

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

工作正常,但现在我想在此类中创建导航属性 Player 和 User。我有这个 Fluent 代码:

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

但我只收到此错误消息,而且我不知道出了什么问题。

类型中的每个属性名称都必须是唯一的。属性名称“用户名” 已经定义了。

我的数据库设置看起来像类,在玩家表中名称是唯一的。它在 User 表中不是唯一的。用户可以在没有玩家的情况下存在,反之亦然。 (实际上我不想要 Player 类中的任何 User 属性,但我认为这是一个要求?!)

最佳答案

我认为它在提示 UserName 已经是对象模型中的属性这一事实。请参阅 Map() 方法的文档:

来自 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.

关于c# - EF映射一对一可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12228262/

相关文章:

c# - 我应该使用哪些技术将 iOS 客户端应用程序链接到 asp.net 后端以进行安全通信?

c# - DbContext 已被处置(ASP.NET MVC)

c# - 集成测试 Entity Framework ,需要不同的数据库服务器吗?

c# - 如何根据复选框状态更改列表框选择,反之亦然?

c# - 使用 MVVM 绑定(bind) : Property not found in Xamarin. 表单

c# - 如何让应用程序通过wcf数据服务使用不同的数据库

php - 在条件发生时跳出循环,并避免使用其预设的 db 值

php - mysql错误: Mysql server has gone away

php - MySQL ROLLBACK 并与 PHP 一起使用

c# - 给定当前记录的 ID,这是选择上一条和下一条记录的正确方法吗?