对象关系映射 [英] object-relational mapping

查看:204
本文介绍了对象关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个复杂的数据类型的实例存储到关系数据库。 有没有办法做到这一点不先moddeling数据库结构,就像是在ado.net做了什么?数据库(或表)结构应该从类结构来创建。这个类有喜欢整数,字符串或布尔变量的一些特性,但也可能有更复杂的。 我要感谢每一个帮助的建议......

I need to store an instance of an complex datatype into a relational database. Is there a way to do this without moddeling the database structure first, like it is done in ado.net? The database (or table) structure should be created from the class structure. The class has some properties like ints, strings or bools but could also have more complex ones. I am grateful for every helping advice...

我想这样做在C#...

I want to do this in c#...

更新:

感谢您的回信。我想code第一EF 4(非常感谢你拉梅什),并得到了我的对象到数据库中。但现在我有一个问题,以获取数据的分贝回对象的实例。我的类看起来像:

Thank you for your replys. I tried "code first" of EF 4 (Thank you very much Ramesh) and got my objects into the database. But now I am having a problem to get the data out of the db back to the instance of an object. My classes look like that:

class Foo {
    public int id { get; set; }
    public string Woot { get; set; } 
}

class Bar {
    public int id { get; set; }
    public string name { get; set; }
    public ICollection<Foo> FooList { get; set; }
}

所以,我说我可以创建这些类的实例,并将其写入到数据库。但是,当我尝试从数据库数据的新情况下,仅仅是int和棒式恢复的字符串,但FOOS的收藏是EMPTY的。我的数据库环境是这样的:

So, as I said i can create instances of these classes and write them to the db. But when I try to create new instances from the db data, just the int and the string of the "Bar"type is recovered but the Collection of Foos is emtpy. My db context looks like this:

class DBstructure: DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
}


任何想法?


any Idea?

推荐答案

是多数民众赞成在可能的,你必须使用实体框架4本。该方法被称为code第一。

Yes thats possible, you have to use Entity framework 4 for this. the approach is called "Code First".

您可以在ScottGu的帖子阅读很多关于这个在这里<一个href="http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx"相对=nofollow> code-一是发展与实体框架4

you can read lot about this here in ScottGu's post Code-First Development with Entity Framework 4

这篇关于对象关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆