Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架1:1映射外键_Entity Framework_Ef Code First_Entity Framework Migrations - Fatal编程技术网

Entity framework 实体框架1:1映射外键

Entity framework 实体框架1:1映射外键,entity-framework,ef-code-first,entity-framework-migrations,Entity Framework,Ef Code First,Entity Framework Migrations,我的实体AppUser有一个可选的UserProfile,UserProfile是必需的AppUser。我想要一把彼此的外键 public class AppUser { public int Id { get; set; } public string Name { get; set; } public UserProfile UserProfile { get; set; } public int? UserProf

我的实体AppUser有一个可选的UserProfile,UserProfile是必需的AppUser。我想要一把彼此的外键

public class AppUser
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public UserProfile UserProfile { get; set; }
        public int? UserProfileId { get; set; }
    }

public class UserProfile
    {
        public int Id { get; set; }

        public string SomeUserProfileValue { get; set; }

        public AppUser AppUser { get; set; }
        public int AppUserId { get; set; }
    }
我得到了这个映射:

modelBuilder.Entity<AppUser>().HasOptional(x => x.UserProfile).WithRequired(x => x.AppUser)
因此,我尝试如下更改映射配置

modelBuilder.Entity<AppUser>().HasOptional(x => x.UserProfile).WithRequired(x => x.AppUser)
                .Map(c => c.MapKey("AppUserId"));
这似乎在抱怨我的模型中已经定义了一个字段AppUserId

这就是我们定义实体的方式,我们总是包括类和id字段,在不同的情况下使用哪一个提供了更多的灵活性

所以我有点被困在这里。。。在模型中定义了类和id字段的同时,有没有办法实现这种1:1的双向关系?
为什么在AppUser表中没有生成可为空的外键?

我自己发现,使用DataAnnotation通常会得到更好的结果。因此:

public class AppUser
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public int? UserProfileId { get; set; }

    [ForeignKey = "UserProfileId"]
    public UserProfile UserProfile { get; set; }

}

public class UserProfile
{
    [Key]
    public int Id { get; set; }

    public string SomeUserProfileValue { get; set; }

    public int AppUserId { get; set; }

    [ForeignKey = "AppUserId"]
    public AppUser AppUser { get; set; }

}

此提示错误:无法确定类型“UserProfile”和“AppUser”之间关联的主体端。必须使用关系fluent API或数据批注显式配置此关联的主体端。如果我在UserProfile.AppUser上添加了必需的属性,则得到:类型“AppUser”的属性“UserProfile”上的ForeignKeyAttribute无效。在依赖类型“UserProfile”上找不到外键名“UserProfileId”。Name值应该是一个以逗号分隔的外键属性名列表。Sry,关于这个。现在远离电脑@GuillaumeMorin,请尝试将[Required]添加到UserProfile.AppUserId。
AppUserId: Name: Each property name in a type must be unique. Property name 'AppUserId' is already defined.
public class AppUser
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public int? UserProfileId { get; set; }

    [ForeignKey = "UserProfileId"]
    public UserProfile UserProfile { get; set; }

}

public class UserProfile
{
    [Key]
    public int Id { get; set; }

    public string SomeUserProfileValue { get; set; }

    public int AppUserId { get; set; }

    [ForeignKey = "AppUserId"]
    public AppUser AppUser { get; set; }

}