实体框架代码首先不起作用

发布于 2024-11-16 05:51:34 字数 1350 浏览 2 评论 0原文

我有一个这样的类:

class ClassA
{
    public long classAID {get; set;}
    public string Description {get; set;}
    public IEnumerable<ClassB> ClassBs {get; set;}
}

class ClassB
{
    public long classBID {get; set;} 
    public string SomeOtherDescription {get; set;}

    public IEnumerable<ClassA> {get; set;}
}
class TestContext: DBContext
{
    public DbSet<ClassA> ClassAs {get; set;}
    public DbSet<ClassB> ClassBs {get; set;}
}

H 具有与类和属性相同的列名和表名的数据库。 我已经按照要求完成了web.config配置。当我尝试使用上面的方法检索数据时,我收到错误

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

,并且

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

我厌倦了多种方法,例如设置键属性、外键属性等,但没有任何效果。请让我知道我错过了什么。

我使用 C# 4,并且已使用以下 URL 进行验证:
http://www.asp.net/mvc/tutorials/ mvc-music-store-part-4

http:// weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

I have a Class like this:

class ClassA
{
    public long classAID {get; set;}
    public string Description {get; set;}
    public IEnumerable<ClassB> ClassBs {get; set;}
}

class ClassB
{
    public long classBID {get; set;} 
    public string SomeOtherDescription {get; set;}

    public IEnumerable<ClassA> {get; set;}
}
class TestContext: DBContext
{
    public DbSet<ClassA> ClassAs {get; set;}
    public DbSet<ClassB> ClassBs {get; set;}
}

H have the DB with same column names and table names as the classes and properties.
I have done the web.config configuration as required. When i try to use above to retrieve the data i get the error

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

and

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

I tired multiple approaches such as setting the key attribute, Foreign key attribute etc. but nothing worked. Please let me know what am i missing.

I use C# 4 and i have verified with following URLs:
http://www.asp.net/mvc/tutorials/mvc-music-store-part-4

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

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

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

发布评论

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

评论(1

小清晰的声音 2024-11-23 05:51:34

使用这个:

public class ClassA
{
    public long ClassAID {get; set;}
    public string Description {get; set;}
    public virtual ICollection<ClassB> ClassBs {get; set;}
}

public class ClassB
{
    public long ClassBID {get; set;} 
    public string SomeOtherDescription {get; set;}
    public virtual ICollection<ClassA> {get; set;}
}

public class TestContext: DBContext
{
    public DbSet<ClassA> ClassAs { get; set; }
    public DbSet<ClassB> ClassBs { get; set; }
}

正如您所看到的,导航属性被标记为虚拟。它将允许延迟加载=导航属性将在您的代码第一次访问该属性时单独加载。这种方式并不总是加载导航属性的最佳方法,因为它会导致数据库的额外往返。因此,EF 还提供急切加载功能,您可以在其中明确告诉 EF 加载导航属性:

var query = context.ClassAs.Include(c => ClassBs);

Use this:

public class ClassA
{
    public long ClassAID {get; set;}
    public string Description {get; set;}
    public virtual ICollection<ClassB> ClassBs {get; set;}
}

public class ClassB
{
    public long ClassBID {get; set;} 
    public string SomeOtherDescription {get; set;}
    public virtual ICollection<ClassA> {get; set;}
}

public class TestContext: DBContext
{
    public DbSet<ClassA> ClassAs { get; set; }
    public DbSet<ClassB> ClassBs { get; set; }
}

As you can see navigation properties are marked as virtual. It will allow lazy loading = navigation property will be loaded separately first time your code access the property. It is not always the best way to load navigation properties this way because it causes additional roundtrips to the database. Because of that EF also offers eager loading where you explicitly tell EF to load your navigation property:

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