ppetrov
ppetrov

Reputation: 3105

Generate Guid on database but only when not set in entity

I have a case where I need to add a Guid Property that is NOT the primary key, and that could be shared with several objects in the table.

What I'd like to do is:

Both of this would be done on Insert only, Updates won't touch these values.

What I have tried:

I've seen quite a lot about this topic, and the closest thing would be this article: http://www.davepaquette.com/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx

But we don't (and won't) use Migration in our project, so that doesn't seem fit.

or this SO question, but that would mean generating Guids in .Net (which doesn't seem to be very clean, at least in my opinion): EF, Code First - How to set a custom Guid identity value on insert

Is there a way to generate the Guid Database side, AND set it when I need to in EF Code first?

If not, what would be a good alternative? Is it really a bad idea to generate Guids on the .Net side? (I could go with that if nothing else is possible)

Upvotes: 2

Views: 1235

Answers (1)

Monah
Monah

Reputation: 6784

I will assume that you are using MS-SQL , then you can do the following

To use the execute command

public class YourDbContext: DbContext
{
     public YourDbContext():base("ConnectionString")
     {              
            if (Database.Exists())
            {                   
                Database.ExecuteSqlCommand("if object_id('CT_DefaultGuid') is null alter table YourTable add constraint CT_DefaultGuid default newid() for YourColumn");
            }
     }
}

To set the Id from .Net, you can do the following

  • Create a Base Entity contains Id property
  • In the constructor you check if the Id is empty then initialize it
  • Let all the entities you have to inherits from this class

Your base class should look like

public class BaseEntity
{
     public BaseEntity()
     {
          if(Id==Guid.Empty)
               Id = Guid.NewGuid();
     }

     public Guid Id{get;set;}
}

To use the migration for existing database

  • from PMC => Enable-migrations
  • from PMC => Add-Migration "FirstRun"
  • open the generated migration file and make sure to empty the Up and Down methods ( this will not apply any changes on the database)
  • Add the corresponding alter column fluent code using Sql("") method in the Up method
  • from PMC => Update-Database -Script , to make sure that only sql statement generated is the alter table statement
  • from PMC => once you are sure that the desired statement is the only appearing in the SQL script , then apply : Update-Database.

Your class should like this

public class FirstRun : DbMigration
{    public override void Up()
     {
        Sql("alter table YourTable add constraint CT_DefaultGuid default newid() for YourColumn");
     }
}

I recommend the last approach, it will be executed once, and you can add changes later to your database.

Hope this will help you

Upvotes: 1

Related Questions