简体   繁体   English

实体框架中的 1:1 关系

[英]1:1 relationship in Entity framework

I need 1:1 relationship between ApplicationUser and my own class in entity framework.我需要ApplicationUser和实体框架中我自己的类之间的 1:1 关系。

I do this:我这样做:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    /*Realations*/

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual Posts Post { get; set; }
}


public class Posts : System.Object
{
    public Posts()
    {
        this.PostDate = DateTime.Now;
        this.PostViews = 0;
        this.PostPic = "d.jpg";
    }

    [Key]
    public int PostID { get; set; }

    public string PostName { get; set; }
    public string PostSummery { get; set; }
    public string PostDesc { get; set; }
    public string PostPic { get; set; }
    public DateTime PostDate { get; set; }
    public int PostViews { get; set; }
    public string postMetaKeys { get; set; }
    public string PostMetaDesc { get; set; }


    public string UserId { get; set; }
    [Key, ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }


    public int CategoryID { get; set; }
    [Key, ForeignKey("CategoryID")]
    public virtual Categories Category { get; set; }

    public virtual ICollection<Comment> commnets {get; set;}
}

But I am getting an exception when I write the command "add-migration relation" inside Nuget console.但是当我在Nuget控制台中编写命令“添加迁移关系”时出现异常。

Unable to determine the principal end of an association between the types 'FinalKaminet.Models.ApplicationUser' and 'Models.Posts'.无法确定类型“FinalKaminet.Models.ApplicationUser”和“Models.Posts”之间关联的主要端。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.必须使用关系流畅 API 或数据注释显式配置此关联的主体端。

I also add below code inside IdentityModels , but another error was shown:我还在IdentityModels添加了以下代码,但显示了另一个错误:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); 


        modelBuilder.Entity<ApplicationUser>()
        .HasOptional(f => f.Post)
        .WithRequired(s => s.ApplicationUser);
    }

One or more validation errors were detected during model generation:在模型生成期间检测到一个或多个验证错误:

ApplicationUser_Post_Target: : Multiplicity is not valid in Role 'ApplicationUser_Post_Target' in relationship 'ApplicationUser_Post'. ApplicationUser_Post_Target::多重性在关系“ApplicationUser_Post”中的角色“ApplicationUser_Post_Target”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.由于从属角色属性不是关键属性,因此从属角色的重数上限必须为“*”。

What is wrong?怎么了?

you want a 1 to 1 relation between user and post ?您想要用户和帖子之间的 1 对 1 关系吗? A user can only post one, and only, post ?一个用户只能发布一个,并且只能发布 ?

Anyway, in EF (at least 6) a 1 to 1 relation can be established between two entities sharing the same PK.无论如何,在 EF(至少 6 个)中,可以在共享相同 PK 的两个实体之间建立 1 对 1 的关系。 That is the PK is the FK.那就是PK就是FK。 So you must set the PK of posts as a string.所以你必须将posts的PK设置为字符串。

Otherwise you are in a 1 to * relation.否则,您处于 1 到 * 关系中。

Add [Required] attribute to UserId property in Posts class.[Required]属性添加到Posts类中的UserId属性。

By default, a string property would be nullable but as this is the foreign key and you want the relationship to be Required, you'll have to set the UserId property as not-nullable which can be done by adding [Required] attribute.默认情况下,字符串属性可以为空,但由于这是外键,并且您希望关系为必需,因此您必须将UserId属性设置为不可为空,这可以通过添加[Required]属性来完成。

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

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