sql server - How to make persisted computed column in EF code first? -


how column similar persisted computed column in database?

my current attempt (it loads compcol rows null in seed) :

    public class call     {         public call()         {         }          [key]         public int id { get; set; }          [databasegenerated(databasegeneratedoption.computed)]         public string compcol         {                         {                 return "abc-" + convert.tostring(id).padleft(5, '0');             }             protected set {}         } } 

the solution found :

  1. make sure auto migrations turned off. vs generate script (fluent api code) further customise instead of running it. in configuration class :

    public configuration() {     automaticmigrationsenabled = false; } 
  2. add field class , set computed so, setter private because cannot write computed field :

    [databasegenerated(databasegeneratedoption.computed)] public string breakdownno { get; private set; } 
  3. then add-migration [xyz-name] in package manager console generate migration code, appear under migrations folder given name.

  4. inside migration comment out code in up() , add custom sql :

    public override void up() {     //addcolumn("dbo.calls", "breakdownno", c => c.string());     sql("alter table dbo.calls add breakdownno ('bd'+right('00000'+ cast(id varchar), 6))"); } 
  5. do update-database in pm , should add computed column properly.

further notes : if formula wrong have revert migration doing update-database -targetmigration: [name of migration go to] add-migration name , amend formula there, finishing off update-database. there may better way found , used.

i did not find way make field persisted yet.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -