MVC - 实体框架 - 元数据关系

发布于 2024-12-22 10:19:53 字数 1142 浏览 7 评论 0原文

今天我第一次使用 MVC。通常我首先使用带有模型的 EF,但我想尝试 POCO。

所以我已经创建了 3 个实体,当我尝试创建控制器时出现错误:

无法检索“BookExchange.Models.Exchange”的元数据。无法确定类型“BookExchange.Models.Exchange”和“BookExchange.Models.Book”之间关联的主体端。此关联的主体必须使用关系流畅 API 或数据注释显式配置。

我的三堂课:

public class Book
{
    public int BookID { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN10 { get; set; }

    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public virtual Exchange Exchange { get; set; }
}

public class Person
{
    public int PersonID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; }
}

public class Exchange
{
    [Key]
    public int BookID { get; set; }

    public int PersonID { get; set; }
    public DateTime ReturnDate { get; set; }

    public virtual Person Person { get; set; }
    public virtual Book Book { get; set; }
}

有谁知道我做错了什么?我不想失去关联属性。

提前致谢!

Today I've been working with MVC for the first time. Also normally I use the EF with model first, but I wanted to try POCO.

So I've made my 3 entities and when I try to make a controller I get an error:

Unable to retrieve metadata for "BookExchange.Models.Exchange". Unable to determine the principal end of an association between the types "BookExchange.Models.Exchange" and "BookExchange.Models.Book". The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

My 3 classes:

public class Book
{
    public int BookID { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN10 { get; set; }

    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public virtual Exchange Exchange { get; set; }
}

public class Person
{
    public int PersonID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; }
}

public class Exchange
{
    [Key]
    public int BookID { get; set; }

    public int PersonID { get; set; }
    public DateTime ReturnDate { get; set; }

    public virtual Person Person { get; set; }
    public virtual Book Book { get; set; }
}

Does anyone know what I'm doing wrong? I don't want to lose the association properties.

Thanks in advance!

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

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

发布评论

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

评论(3

云归处 2024-12-29 10:19:53

尝试添加外键属性以供参考。例如

public class Book
{
    public int BookID { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN10 { get; set; }

    public virtual Person Person { get; set; }
    public int PersonID { get; set; }
    public virtual Exchange Exchange { get; set; }
    public int ExchangeID { get; set; }
}

public class Person
{
    public int PersonID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; } 
}

public class Exchange
{
    public int ExchangeID { get; set; }

    public int BookID { get; set; }
    public virtual Book Book { get; set; }
    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public DateTime ReturnDate { get; set; }
}

,另外,看看 ScottGu 关于代码优先的帖子这篇关于约定的 EF 帖子

Try adding foreign key properties for your references. E.g.

public class Book
{
    public int BookID { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN10 { get; set; }

    public virtual Person Person { get; set; }
    public int PersonID { get; set; }
    public virtual Exchange Exchange { get; set; }
    public int ExchangeID { get; set; }
}

public class Person
{
    public int PersonID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; } 
}

public class Exchange
{
    public int ExchangeID { get; set; }

    public int BookID { get; set; }
    public virtual Book Book { get; set; }
    public int PersonID { get; set; }
    public virtual Person Person { get; set; }

    public DateTime ReturnDate { get; set; }
}

Also, take a look at ScottGu's post on code first and this EF post on conventions.

℡Ms空城旧梦 2024-12-29 10:19:53

试试这个:(删除数据库,这样 EF 将创建新的)

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN { get; set; }

    public virtual Person Person { get; set; }
    public virtual Exchange Exchange { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; }
}

public class Exchange
{    
    public int Id { get; set; }   
    public DateTime ReturnDate { get; set; }

    public virtual Person Person { get; set; }
    public virtual Book Book { get; set; }
}

Try this: (Remove database, so EF will create new)

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string ISBN { get; set; }

    public virtual Person Person { get; set; }
    public virtual Exchange Exchange { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Book> Books { get; set; }
    public virtual ICollection<Exchange> Exchanges { get; set; }
}

public class Exchange
{    
    public int Id { get; set; }   
    public DateTime ReturnDate { get; set; }

    public virtual Person Person { get; set; }
    public virtual Book Book { get; set; }
}
滥情哥ㄟ 2024-12-29 10:19:53

这是您的一对一协会。

删除交换或书籍之间的一个引用,因此代码优先可以决定在您的一对一关系中哪一个更重要(书籍<-->交换)

如果您想知道原因,您应该阅读以下内容:

It's your one on one associations.

Remove one reference between exchange or book, so Code-first can decide which one is more important in your one on one relation (Book <--> Exchange)

If you want to know why, you should read this:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文