Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 代码优先-两个外键作为主键,无法添加迁移_Entity Framework_Ef Code First_Entity Framework Migrations - Fatal编程技术网

Entity framework 代码优先-两个外键作为主键,无法添加迁移

Entity framework 代码优先-两个外键作为主键,无法添加迁移,entity-framework,ef-code-first,entity-framework-migrations,Entity Framework,Ef Code First,Entity Framework Migrations,我的用户表: public class User { [Key] public int UserId { get; set; } public virtual ICollection<PollVote> PollVotes { get; set; } } 我的配置: //User config: this.HasMany(x => x.PollVotes)

我的用户表:

public class User
    {
        [Key]
        public int UserId { get; set; }

        public virtual ICollection<PollVote> PollVotes { get; set; }
    }
我的配置:

//User config:
             this.HasMany(x => x.PollVotes)
                .WithRequired()
                .HasForeignKey(x => x.UserId)
                .WillCascadeOnDelete(false);

//Poll Config
            this.HasMany(x => x.PollVotes)
                .WithRequired()
                .HasForeignKey(x => x.PollId)
                .WillCascadeOnDelete(false);

//PollVote Config
            this.HasKey(x => x.UserId)
                .HasRequired(x => x.User)
                .WithMany()
                .HasForeignKey(x => x.UserId);
            this.HasKey(x => x.PollId)
                .HasRequired(x => x.Poll)
                .WithMany()
                .HasForeignKey(x => x.PollId);
这种关系是:一次投票可以有很多票,但用户只能对每次投票投一票

我在PM控制台中尝试添加迁移时出现此错误

\tSystem.Data.Entity.Edm.edmsociationEnd::多重性在关系“PollVote\u Poll”中的角色“PollVote\u Poll\u Source”中无效。因为依赖角色引用密钥属性,所以依赖角色的多重性上限必须为“1”。 \tSystem.Data.Entity.Edm.edmsociationEnd::多重性在关系“Poll\u pollvoces”中的角色“Poll\u pollvoces\u Target”中无效。因为依赖角色引用密钥属性,所以依赖角色的多重性上限必须为“1”


有什么建议吗?

您可以通过向数据批注添加
[Column]
属性来指定复合键

    [Key, Column(Order = 1)]
    public int PollId { get; set; }

    [Key, Column(Order = 2)]
    public int UserId { get; set; }
…或使用具有Fluent API的匿名对象:

this.HasKey(x => new { x.UserId, x.PollId });

this.HasRequired(x => x.User)
    .WithMany(u => u.PollVotes)
    .HasForeignKey(x => x.UserId);

this.HasRequired(x => x.Poll)
    .WithMany(p => p.PollVotes)
    .HasForeignKey(x => x.PollId);

如上图所示,不要忘记
WithMany
中反向导航属性的lambda表达式,并删除
UserConfig
PollConfig

中的冗余配置,这会使数据库生成的VoteId字段冗余吗?@Colin:为了定义键和关系,是的。但是,不管出于什么原因,他可能希望有一个独特的计数器来对投票进行编号。
    [Key, Column(Order = 1)]
    public int PollId { get; set; }

    [Key, Column(Order = 2)]
    public int UserId { get; set; }
this.HasKey(x => new { x.UserId, x.PollId });

this.HasRequired(x => x.User)
    .WithMany(u => u.PollVotes)
    .HasForeignKey(x => x.UserId);

this.HasRequired(x => x.Poll)
    .WithMany(p => p.PollVotes)
    .HasForeignKey(x => x.PollId);