Object relational mapping

I need to save an instance of a complex data type to a relational database. Is there a way to do this without first changing the database structure, as is done in ado.net? The structure of the database (or table) must be created from the structure of the class. A class has some properties, such as ints, string, or bools, but may also have more complex ones. I am grateful for every help advice ...

I want to do this in C # ...

Update:

Thank you for your responses. I tried the "code first" EF 4 (Thanks a lot to Ramesh) and got my objects in the database. But now I have a problem to return data from db back to the object instance. My classes look like this:

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; } } 

So, as I said, I can instantiate these classes and write them to db. But when I try to create new instances from the db data, only int and a string of type β€œBar” are restored, but Collection of Foos is emtpy. My db context is as follows:

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


any idea?

+4
source share
3 answers

Yes, maybe you need to use Entity framework 4 for this. this approach is called "Code First".

you can read about it here in a post by ScottGu Code-First Development with Entity Framework 4

+2
source

It is best to use an ORM tool that can create a database for you from your domain model (classes).

Take a look at NHibernate , Entity Framework, to mention a few.

0
source

I got what I wanted using Entity Framework 4 with the "first code". To write objects from the classes that I posted above to the database, you just need to create objects regularly:

 Foo foo = new Foo(); foo.Woot = "foo1"; Foo foo2 = new Foo(); foo2.Woot = "foo2"; Bar bar = new Bar(); bar.name = "bar1"; bar.Whee = new List<Foo>(); bar.Whee.Add(foo); bar.Whee.Add(foo2); 

Then create a DbContext object, add other objects to this context and call the saveChanges () method:

 DBstructure dbconn = new DBstructure(); dbconn.Database.Connection.ConnectionString = connectionString //depends on your DB dbconn.Bars.Add(bar); dbconn.Database.Connection.Open(); dbconn.SaveChanges(); dbconn.Database.Connection.Close(); 

This creates a new database with tables "Foos" and "Bars" and another table of links to manage relationships between entities.

Creating objects from a database is also quite simple. Just connect to the database via DbContext, as was done above. Create your objects and set them in db data:

 DBstructure dbconn = new DBstructure(); dbconn.Database.Connection.ConnectionString = connString; dbconn.Foos.ToList(); //seems that you have to do that dbconn.Bars.ToList<Bar>(); Bar barFromDB = new Bar(); barFromDB = dbconn.Bars.First<Bar>(); 

This works well in my application. Sorry for the weird class and variable names. I just copied the code from my test application. Hope that helps someone ... Grüßung Jucker!

0
source

All Articles