Code First CTP5 多对多绑定

发布于 2024-10-17 16:06:54 字数 3860 浏览 1 评论 0原文

我一直在实体框架中使用代码优先方法。我有一个 Event 类、一个 Band 类和一个 EventBands 类,它们映射多对多关系。 Code First 方法运行良好(当我没有 EventBands 类时),但后来我决定使用多对多表来存储附加值。现在我收到此错误消息:

System.Data.Edm.EdmEntityType: : EntityType 'EventBands' 没有定义键。定义此 EntityType 的键。

System.Data.Edm.EdmEntitySet:EntityType:EntitySet EventBands 基于未定义键的 EventBands 类型。

错误消息的含义显而易见。然而,解决方案并不那么明显。我认为我必须重写模型绑定方法,但我不完全确定如何使用这种方法映射键。

任何帮助将不胜感激,我已在下面列出了有问题的课程。

预先感谢,

Jon

Event:

#region Properties
    private int eventId;
    public int EventId
    {
        get
        {
            return eventId;
        }
        set
        {
            eventId = value;
        }
    }

    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    private string description;
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    private DateTime startDatetime;
    public DateTime StartDateTime
    {
        get
        {
            return startDatetime;
        }
        set
        {
            startDatetime = value;
        }
    }

    private DateTime endDatetime;
    public DateTime EndDateTime
    {
        get
        {
            return endDatetime;
        }
        set
        {
            endDatetime = value;
        }
    }

    private int venueUserId;
    public int VenueUserId
    {
        get { return venueUserId; }
        set { venueUserId = value; }
    }

    public virtual Venue Venue
    {
        get;
        set;
    }

    public virtual ICollection<EventReview> Reviews
    {
        get;
        set;
    }

    public virtual ICollection<EventBands> EventBands
    {
        get;
        set;
    }

    public virtual ICollection<Fan> Attendees
    {
        get;
        set;
    }
    #endregion

    #region Constructor
    public Event()
    {
        EventBands = new HashSet<EventBands>();
        Attendees = new HashSet<Fan>();
        StartDateTime = DateTime.Now;
        EndDateTime = DateTime.Now.AddDays(14);
    }
    #endregion

Band:

public class Band : PostableUser
{
    #region Properties
    private int genreGenreId;
    public int GenreGenreId
    {
        get { return genreGenreId; }
        set { genreGenreId = value; }
    }

    public virtual Genre Genre
    {
        get;
        set;
    }

    public virtual ICollection<Album> Albums
    {
        get;
        set;
    }

    public virtual ICollection<BandReview> Reviews
    {
        get;
        set;
    }

    public virtual ICollection<EventBands> EventBands
    {
        get;
        set;
    }
    #endregion

    #region Constructor
    public Band()
    {
        EventBands = new HashSet<EventBands>();
    }
    #endregion
}

EventBands

#region Properties
    private int eventEventId;
    public int EventEventId
    {
        get { return eventEventId; }
        set { eventEventId = value; }
    }

    public virtual Event Event
    {
        get;
        set;
    }

    private int bandUserId;
    public int BandUserId
    {
        get { return bandUserId; }
        set { bandUserId = value; }
    }

    public virtual Band Band
    {
        get;
        set;
    }

    private DateTime startDateTime;
    public DateTime StartDateTime
    {
        get { return startDateTime; }
        set { startDateTime = value; }
    }

    private DateTime endDateTime;
    public DateTime EndDateTime
    {
        get { return endDateTime; }
        set { endDateTime = value; }
    }
    #endregion

BandUserId 继承自 User 基类。

I have been using the Code First approach for the Entity Framework. I have an Event class, a Band class and a EventBands class which maps the many to many relationship. The Code First approach worked fine (When I didn't have the EventBands class) but then I decided I wanted the many to many table to store additional values. Now I get this error message:

System.Data.Edm.EdmEntityType: : EntityType 'EventBands' has no key defined. Define the key for this EntityType.

System.Data.Edm.EdmEntitySet: EntityType: The EntitySet EventBands is based on type EventBands that has no keys defined.

It obvious what the error message means. However the resolution isn't so obvious. I think I have to override the model binding method but I am not entirely sure how to map keys with this approach.

Any help would be appreciated, I have included the classes in question below.

Thanks in advance,

Jon

Event:

#region Properties
    private int eventId;
    public int EventId
    {
        get
        {
            return eventId;
        }
        set
        {
            eventId = value;
        }
    }

    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    private string description;
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    private DateTime startDatetime;
    public DateTime StartDateTime
    {
        get
        {
            return startDatetime;
        }
        set
        {
            startDatetime = value;
        }
    }

    private DateTime endDatetime;
    public DateTime EndDateTime
    {
        get
        {
            return endDatetime;
        }
        set
        {
            endDatetime = value;
        }
    }

    private int venueUserId;
    public int VenueUserId
    {
        get { return venueUserId; }
        set { venueUserId = value; }
    }

    public virtual Venue Venue
    {
        get;
        set;
    }

    public virtual ICollection<EventReview> Reviews
    {
        get;
        set;
    }

    public virtual ICollection<EventBands> EventBands
    {
        get;
        set;
    }

    public virtual ICollection<Fan> Attendees
    {
        get;
        set;
    }
    #endregion

    #region Constructor
    public Event()
    {
        EventBands = new HashSet<EventBands>();
        Attendees = new HashSet<Fan>();
        StartDateTime = DateTime.Now;
        EndDateTime = DateTime.Now.AddDays(14);
    }
    #endregion

Band:

public class Band : PostableUser
{
    #region Properties
    private int genreGenreId;
    public int GenreGenreId
    {
        get { return genreGenreId; }
        set { genreGenreId = value; }
    }

    public virtual Genre Genre
    {
        get;
        set;
    }

    public virtual ICollection<Album> Albums
    {
        get;
        set;
    }

    public virtual ICollection<BandReview> Reviews
    {
        get;
        set;
    }

    public virtual ICollection<EventBands> EventBands
    {
        get;
        set;
    }
    #endregion

    #region Constructor
    public Band()
    {
        EventBands = new HashSet<EventBands>();
    }
    #endregion
}

EventBands

#region Properties
    private int eventEventId;
    public int EventEventId
    {
        get { return eventEventId; }
        set { eventEventId = value; }
    }

    public virtual Event Event
    {
        get;
        set;
    }

    private int bandUserId;
    public int BandUserId
    {
        get { return bandUserId; }
        set { bandUserId = value; }
    }

    public virtual Band Band
    {
        get;
        set;
    }

    private DateTime startDateTime;
    public DateTime StartDateTime
    {
        get { return startDateTime; }
        set { startDateTime = value; }
    }

    private DateTime endDateTime;
    public DateTime EndDateTime
    {
        get { return endDateTime; }
        set { endDateTime = value; }
    }
    #endregion

BandUserId is inherited from the User base class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-10-24 16:06:54

我最终通过重写 DataContext 类中的 OnModelCreating 方法来实现此目的。

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AlbumConfiguration());
        modelBuilder.Configurations.Add(new BandConfiguration());
        modelBuilder.Configurations.Add(new CityConfiguration());
        modelBuilder.Configurations.Add(new CommentConfiguration());
        modelBuilder.Configurations.Add(new CountryConfiguration());
        modelBuilder.Configurations.Add(new CountyConfiguration());
        modelBuilder.Configurations.Add(new EventBandsConfiguration());
        modelBuilder.Configurations.Add(new EventFansConfiguration());
        modelBuilder.Configurations.Add(new EventConfiguration());
        modelBuilder.Configurations.Add(new FanConfiguration());
        modelBuilder.Configurations.Add(new GenreConfiguration());
        modelBuilder.Configurations.Add(new PostConfiguration());
        modelBuilder.Configurations.Add(new RecordLabelConfiguration());
        modelBuilder.Configurations.Add(new ReviewConfiguration());
        modelBuilder.Configurations.Add(new TrackConfiguration());
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new VenueConfiguration());
    }

配置类包含主键、组合键、外键和任何其他属性的映射。

事件配置:

public class EventConfiguration : EntityTypeConfiguration<Event>
{
    public EventConfiguration()
    {
        #region Primary Key
        this.HasKey(x => x.EventId);
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.Venue)
            .WithMany()
            .HasForeignKey(x => x.VenueId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.Description).IsRequired().HasColumnType("nvarchar");
        this.Property(x => x.EndDateTime).IsRequired();
        this.Property(x => x.Name).IsRequired().HasColumnType("nvarchar");
        this.Property(x => x.StartDateTime).IsRequired();
        #endregion
    }
}

事件频段配置:

public class EventBandsConfiguration : EntityTypeConfiguration<EventBands>
{
    public EventBandsConfiguration()
    {
        #region Primary Key
        this.HasKey(x => new { x.BandId, x.EventId });
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.Band)
            .WithMany()
            .HasForeignKey(x => x.BandId)
            .WillCascadeOnDelete(false);
        this.HasRequired(x => x.Event)
            .WithMany()
            .HasForeignKey(x => x.EventId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.StartDateTime).IsRequired();
        this.Property(x => x.EndDateTime).IsRequired();
        #endregion
    }
}

用户配置:

public UserConfiguration()
    {
        #region Primary Key
        this.HasKey(x => x.UserId);
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.City)
            .WithMany()
            .HasForeignKey(x => x.CityId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.UserName);
        this.Property(x => x.LoweredUserName);
        this.Property(x => x.ApplicationName);
        this.Property(x => x.Email);
        this.Property(x => x.Comment);
        this.Property(x => x.Password);
        this.Property(x => x.PasswordQuestion);
        this.Property(x => x.PasswordAnswer);
        this.Property(x => x.IsApproved);
        this.Property(x => x.LastActivityDate);
        this.Property(x => x.LastLoginDate);
        this.Property(x => x.LastPasswordChangedDate);
        this.Property(x => x.CreationDate);
        this.Property(x => x.IsOnline);
        this.Property(x => x.IsLockedOut);
        this.Property(x => x.LastLockedOutDate);
        this.Property(x => x.FailedPasswordAttemptCount);
        this.Property(x => x.FailedPasswordAttemptWindowStart);
        this.Property(x => x.FailedPasswordAnswerAttemptCount);
        this.Property(x => x.FailedPasswordAnswerAttemptWindowStart);
        this.Property(x => x.MobileAlias);
        this.Property(x => x.IsAnonymous);
        this.Property(x => x.Description);
        this.Property(x => x.Website);
        #endregion

        #region Inheritance Mapping
        this.Map<Fan>(x => x.Requires("UserType").HasValue("Fan"))
            .Map<Band>(x => x.Requires("UserType").HasValue("Band"))
            .Map<Venue>(x => x.Requires("UserType").HasValue("Venue"));
        #endregion
    }
}

频段配置

public class BandConfiguration : EntityTypeConfiguration<Band>
{
    public BandConfiguration()
    {
        #region Foreign Keys
        this.HasRequired(x => x.Genre)
            .WithMany()
            .HasForeignKey(x => x.GenreId)
            .WillCascadeOnDelete(false);
        #endregion
    }
}

I got this working in the end by overriding the OnModelCreating method in my DataContext class.

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AlbumConfiguration());
        modelBuilder.Configurations.Add(new BandConfiguration());
        modelBuilder.Configurations.Add(new CityConfiguration());
        modelBuilder.Configurations.Add(new CommentConfiguration());
        modelBuilder.Configurations.Add(new CountryConfiguration());
        modelBuilder.Configurations.Add(new CountyConfiguration());
        modelBuilder.Configurations.Add(new EventBandsConfiguration());
        modelBuilder.Configurations.Add(new EventFansConfiguration());
        modelBuilder.Configurations.Add(new EventConfiguration());
        modelBuilder.Configurations.Add(new FanConfiguration());
        modelBuilder.Configurations.Add(new GenreConfiguration());
        modelBuilder.Configurations.Add(new PostConfiguration());
        modelBuilder.Configurations.Add(new RecordLabelConfiguration());
        modelBuilder.Configurations.Add(new ReviewConfiguration());
        modelBuilder.Configurations.Add(new TrackConfiguration());
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new VenueConfiguration());
    }

The Configuration classes contain the mappings of the primary, composite keys, foreign keys, and any other properties.

Event Configuration:

public class EventConfiguration : EntityTypeConfiguration<Event>
{
    public EventConfiguration()
    {
        #region Primary Key
        this.HasKey(x => x.EventId);
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.Venue)
            .WithMany()
            .HasForeignKey(x => x.VenueId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.Description).IsRequired().HasColumnType("nvarchar");
        this.Property(x => x.EndDateTime).IsRequired();
        this.Property(x => x.Name).IsRequired().HasColumnType("nvarchar");
        this.Property(x => x.StartDateTime).IsRequired();
        #endregion
    }
}

Event Bands Configuration:

public class EventBandsConfiguration : EntityTypeConfiguration<EventBands>
{
    public EventBandsConfiguration()
    {
        #region Primary Key
        this.HasKey(x => new { x.BandId, x.EventId });
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.Band)
            .WithMany()
            .HasForeignKey(x => x.BandId)
            .WillCascadeOnDelete(false);
        this.HasRequired(x => x.Event)
            .WithMany()
            .HasForeignKey(x => x.EventId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.StartDateTime).IsRequired();
        this.Property(x => x.EndDateTime).IsRequired();
        #endregion
    }
}

User Configuration:

public UserConfiguration()
    {
        #region Primary Key
        this.HasKey(x => x.UserId);
        #endregion

        #region Foreign Keys
        this.HasRequired(x => x.City)
            .WithMany()
            .HasForeignKey(x => x.CityId)
            .WillCascadeOnDelete(false);
        #endregion

        #region Properties
        this.Property(x => x.UserName);
        this.Property(x => x.LoweredUserName);
        this.Property(x => x.ApplicationName);
        this.Property(x => x.Email);
        this.Property(x => x.Comment);
        this.Property(x => x.Password);
        this.Property(x => x.PasswordQuestion);
        this.Property(x => x.PasswordAnswer);
        this.Property(x => x.IsApproved);
        this.Property(x => x.LastActivityDate);
        this.Property(x => x.LastLoginDate);
        this.Property(x => x.LastPasswordChangedDate);
        this.Property(x => x.CreationDate);
        this.Property(x => x.IsOnline);
        this.Property(x => x.IsLockedOut);
        this.Property(x => x.LastLockedOutDate);
        this.Property(x => x.FailedPasswordAttemptCount);
        this.Property(x => x.FailedPasswordAttemptWindowStart);
        this.Property(x => x.FailedPasswordAnswerAttemptCount);
        this.Property(x => x.FailedPasswordAnswerAttemptWindowStart);
        this.Property(x => x.MobileAlias);
        this.Property(x => x.IsAnonymous);
        this.Property(x => x.Description);
        this.Property(x => x.Website);
        #endregion

        #region Inheritance Mapping
        this.Map<Fan>(x => x.Requires("UserType").HasValue("Fan"))
            .Map<Band>(x => x.Requires("UserType").HasValue("Band"))
            .Map<Venue>(x => x.Requires("UserType").HasValue("Venue"));
        #endregion
    }
}

Band Configuration

public class BandConfiguration : EntityTypeConfiguration<Band>
{
    public BandConfiguration()
    {
        #region Foreign Keys
        this.HasRequired(x => x.Genre)
            .WithMany()
            .HasForeignKey(x => x.GenreId)
            .WillCascadeOnDelete(false);
        #endregion
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文