views:

22

answers:

1

Hi,

Is it possible to have a class X and create multiple tables from it using the Entity Framework 4, code first, during runtime?

For example class Item. I want to create table ItemA and table ItemB. Can this be done at runtime?

+1  A: 

Well, actually you can create multiple tables from one EntityConfiguration. But why would you do that?

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

 public class ItemConfig : EntityConfiguration<Item>
 {
     public ItemConfig()
     {
         Property(it => it.Id).IsIdentity();

         Property(it => it.Name).IsUnicode().IsRequired().HasMaxLength(100);

         MapSingleType(c => new { c.Id, c.Name }).ToTable("dbo.ItemA");
         MapSingleType(c => new { c.Id, c.Name }).ToTable("dbo.ItemB");
     }
 }
Yury Tarabanko
a seperate table for the history?Suppose table 'Client' and table 'ClientHistory'. Every edit get's an entry in the history table. The two tables would be almost identical.
Jeroen
I don't think that provided solution would be useful for this case. CreateDatabase() would create to different tables but I haven't try to use such a mapping.
Yury Tarabanko