开发者

Add a new record to existing table in the database on a new entity creation using Entity Framework

I am making a website using ASP.NET MVC 3 and Entity Framework. I want to create a database based on following code:



    public abstract class Document
    {
       public int DocId {get; set;}
       public DocumentType DocType {get; set;}
       public DateTime DateCreated {get; set;}
       public DateTime DateUpdated {get; set;}

    }

      public class DocumentType{
      public int TypeId {get; set;}
      public int TypeName {get; set;}
    }


    public class News: Document
    {
       public string Title {get; set;}
       public string Body {get; set;}
    }

Short description: Document is a base entity for content types such as News (and others). News inherits Document. Each document (like News) has its document type. For example News has type called "News".

Question: Is it possible in my case to update the database with Entity Framework Code First by adding new entities that inherit Document entity and on entity creation add a new record to the existing DocumentType table (without dropping the database) ? All I want to achieve when I add a new entity - a new rec开发者_运维问答ord to be added in DocumentType table so this table data to be always up to date.

If it is possible to achieve, could you share some links or bring some code examples ?

Thank you in advance for your help.


Well, sort of... But I am not sure you will really want to.

The issue is not with the DocumentType, that is just a record in a database and you can just insert more records in that table. So, I have ignored that side of it for this (it is just a code table).

The challenge is that if you create a new entity, EF has to be able to store that new Entity in the database so it should probably create a table or something for it, so the database needs to change. EF does not (currently) support changing the database so will prompt you to drop and create the database. Now, provided the new entities have exactly the same fields there is a way you can do this - sort of.

Let me start by explaining that the default way for EF to deal with inheritance is to have a discriminator column in the Documents table to determine what type of object this particular row is. That is not the only way, and I strongly recommend you review http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx to fully understand this before you start on inheritance. There are some ugly gotchas, including some places where EF really doesn't do what you expect when you are using inheritance.

Anyway, so given these classes:

public abstract class Document
{
    public int ID { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }

    public Document()
    {
        DateCreated = DateTime.UtcNow;
        DateUpdated = DateTime.UtcNow;
    }
}

public class News : Document
{
}

public class NotNews : Document
{
}

and this Db Context

class MyContext : DbContext
{
    public DbSet<Document> Documents { get; set; }
}

You will end up with a table called Documents with the following columns:

  • ID
  • DateCreated
  • DateUpdated
  • Title
  • Body
  • Discriminator

If you insert, say, a News and a NotNews then you get two rows in this table, one will have the word "News" in the Discriminator column and one will have the word "NotNews".

Now, if you now add

public class BreakingNews : Document
{
}

and insert one of those then the database will not need to be dropped and re-created and you will get a new row with the word "BreakingNews" in the discriminator column.

So, it works. But, if you add any new properties to any of the new classes then it will require database changes. I believe that even overriding may require database changes but you can test that easily enough.

As I said at the top - you can sort of do this but I am not sure it is a good idea. When I first started with EF I was doing stuff with inheritance and I have undone most of it since as it just causes too many problems in my experience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜