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?
source share