EntityFramework CodeFirst FK关联问题

发布于 2024-10-24 08:02:25 字数 1629 浏览 2 评论 0原文

我正在尝试让 CodeFirst 正常工作,但遇到了外键和相关对象的问题。这可能是由于我不符合“约定”并且尚未了解如何正确覆盖我的设置中的约定。如果可以的话请帮忙。

我的现有数据库中有这两个表:

create table locations
(
ID int identity primary key
, name varchar(50) not null
)

create table meetings
(
ID int identity primary key
, whatever varchar(50) null
, LocationID int not null constraint FK_meetings_LocationID foreign key references locations(ID)
)

模型的设置如下:

public class Location
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Meeting> Meetings { get; set; }
}

public class Meeting
{
    public int ID { get; set; }
    public string Whatever{ get; set; }
    public Location HeldAt { get; set; }
}

最后,我的上下文设置如下:

public class v1Context : DbContext
{
    public v1Context(string ConnectionString)
        : base(ConnectionString)
    {}

    public DbSet<Location> Locations { get; set; }
    public DbSet<Meeting> Meetings { get; set; }

    //protected override void OnModelCreating(DbModelBuilder modelBuilder)
    //{
    //    modelBuilder.Entity<Location>().HasKey(k => k.ID);
    //    modelBuilder.Entity<Meeting>().HasKey(k => k.ID);
    //}
}

并且,我收到以下错误“执行命令定义时发生错误。请参阅有关详细信息的内部异常”.. 使用 InnerExceptin:

InnerException = {"Invalid column name 'HeldAt_ID'."}

我尝试了 OnModelCreating 的各种咒语,但没有成功。看起来很明显,问题在于它不知道如何将“LocationID”FK 正确导航到具有 ID 字段的 Location 类。但我一直无法弄清楚。

不知何故,我认为既然它是“Location”类型,并且 Location 类也独立映射到数据库,并且因为数据库中存在正确的 FK 关系,所以它只会“弄清楚”。

任何帮助将不胜感激。

I'm trying to get CodeFirst working, and am having a problem with foreign keys and related objects. It is likely due to my not matching the "convention" and not yet understanding how to properly override the conventions in my setup. Please help if you can.

I have these two tables in my existing database:

create table locations
(
ID int identity primary key
, name varchar(50) not null
)

create table meetings
(
ID int identity primary key
, whatever varchar(50) null
, LocationID int not null constraint FK_meetings_LocationID foreign key references locations(ID)
)

And the models are set up like so:

public class Location
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Meeting> Meetings { get; set; }
}

public class Meeting
{
    public int ID { get; set; }
    public string Whatever{ get; set; }
    public Location HeldAt { get; set; }
}

Finally, my context is set up like this:

public class v1Context : DbContext
{
    public v1Context(string ConnectionString)
        : base(ConnectionString)
    {}

    public DbSet<Location> Locations { get; set; }
    public DbSet<Meeting> Meetings { get; set; }

    //protected override void OnModelCreating(DbModelBuilder modelBuilder)
    //{
    //    modelBuilder.Entity<Location>().HasKey(k => k.ID);
    //    modelBuilder.Entity<Meeting>().HasKey(k => k.ID);
    //}
}

And, I'm getting the following error "An error occurred while executing the command definition. See the inner exception for details".. With an InnerExceptin of:

InnerException = {"Invalid column name 'HeldAt_ID'."}

I've tried various incantations of OnModelCreating, without any success. It looks obvious that the problem is that it doesn't know how to navigate the "LocationID" FK properly to the Location class with its ID field. But I've been unable to get it figured out.

Somehow I thought that since it was of type "Location" and that the Location class was also independently mapped to the database, and because there were proper FK relationships in the databse, that it would just "figure it out."

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

生活了然无味 2024-10-31 08:02:25

您不使用列的默认名称,因此必须在 OnModelBuilding 中指定它们:

使用此名称将 LocationID 定义为外键列,而不是默认的预期 HeldAt_ID:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Meeting>()
        .HasRequired(m => m.HeldAt)
        .WithMany(l => l.Meetings)
        .Map(m => m.MapKey("LocationID"));
}

You don't use default names for columns so you must specify them in OnModelBuilding:

Use this to define LocationID as your foreign key column instead of default expected HeldAt_ID:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Meeting>()
        .HasRequired(m => m.HeldAt)
        .WithMany(l => l.Meetings)
        .Map(m => m.MapKey("LocationID"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文