将属性映射到列使用.Map生成它(t => t.MapKey(“GroupId”))

时间:2013-11-27 14:00:16

标签: c# entity-framework entity-framework-4 entity-relationship

我正在使用.Net Framework 4.0开发一个实体框架代码优先(v.4.4.0.0)C#库。

我试图表示谈话可以有零个或一个小组。

有了这两个类:

谈话类:

[DataContract]
public class Talk
{
    [DataMember]
    public int TalkId { get; set; }

    [DataMember]
    public int StarterUserId { get; set; }

    [DataMember]
    public int? RecipientUserId { get; set; }

    [DataMember]
    public int? RecipientGroupId { get; set; }

    public DateTime DateUtcStarted { get; set; }

    [DataMember]
    public string DateStarted
    {
        get
        {
            return DateUtcStarted.ToString("dd/MM/yyyy HH:mm");
        }
        set
        {
            DateUtcStarted = DateTime.Parse(value);
        }
    }

    public User StarterUser { get; set; }
    public User RecipientUser { get; set; }

    public virtual Group RecipientGroup { get; set; }
}

TalkConfiguration 类:

class TalkConfiguration : EntityTypeConfiguration<Talk>
{
    public TalkConfiguration()
    {
        Property(t => t.RecipientGroupId).HasColumnName("GroupId");
        Property(t => t.StarterUserId).IsRequired();
        Property(t => t.RecipientUserId).IsOptional();
        Property(t => t.RecipientGroupId).IsOptional();
        Property(t => t.DateUtcStarted).IsRequired();

        Ignore(t => t.DateStarted);

        HasRequired(t => t.StarterUser).
            WithMany(u => u.TalksStarted).
            HasForeignKey(t => t.StarterUserId);
        HasOptional(t => t.RecipientUser).
            WithMany(u => u.InTalks).
            HasForeignKey(t => t.RecipientUserId);

        HasOptional(t => t.RecipientGroup).WithOptionalDependent(g => g.GroupTalk).Map(t => t.MapKey("GroupId"));
    }
}

我收到此错误消息:

The scheme is invalid. Error 0019: Each property name in a type must be unique.
The property name is already defined.

如果我删除了该属性:

public int? RecipientGroupId { get; set; }

有效。

如何将RecipientGroupId映射到此处生成的数据库列GroupIdHasOptional(t => t.RecipientGroup).WithOptionalDependent(g => g.GroupTalk).Map(t => t.MapKey("GroupId"));

0 个答案:

没有答案