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 :
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; }
add field class , set computed so, setter private because cannot write computed field :
[databasegenerated(databasegeneratedoption.computed)] public string breakdownno { get; private set; }
then
add-migration [xyz-name]
in package manager console generate migration code, appear under migrations folder given name.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))"); }
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
Post a Comment