entity framework - Breeze is trying to update a computed database column -


a friend reported problem computed column, entity framework, , breeze

we have table "fullname" column computed database. when creating new person, breeze sends fullname property value server, though it’s not being set @ all, , triggers error when trying insert new person instance. database throws exception:

the column "fullname" cannot modified because either computed column or result of union operator.

here relevant portion of sql table definition:

 create table [dbo].[person](       [id] [bigint] identity(1,1) not null,       [firstname] [varchar](100) null,       [middlename] [varchar](100) null,       [lastname] [varchar](100) not null,       [fullname]  ((([patient].[lastname]+',') + isnull(' '+[patient].[firstname],'')) + isnull(' '+[patient].[middlename],'')),       ... 

my friend tells me corresponding "code first" class looks this:

 public class person {       public int id {get; set;}       public string firstname {get; set;}       public string middlename {get; set;}       public string lastname {get; set;}       public string fullname {get; set;}       ... } 

the answer question explains problem , offers solution.

design issues

everyone looking @ wonders why there computed column fullname and, secondarily, why property exposed client.

let's assume there reason computed column, reason model value table instead of calculating value itself, , reason send client rather have client calculate it. here's told me that;

"we need include fullname in queries"

life works out way sometimes.

consequences

notice fullname property has public setter. ef metadata generator person class cannot tell read-only property. fullname looks lastname. metadata "this normal read/write property."

breeze doesn't see difference either. client app may not touch property, breeze has send value when creating new person. on server, breeze efcontextprovider thinks should pass value along when creating ef entity. stage set disaster.

what can if (a) can't change table , (b) can't change model's fullname property definition?

a solution

ef needs help. should tell ef database computed property. use ef fluent interface or use attribute shown here:

 [databasegenerated(databasegeneratedoption.computed)] public string fullname { get; set; } 

add attribute , ef knows property read-only. generate appropriate metadata , can save new person cleanly. omit , you'll exception.

note necessary code first. if he'd generated model database first, ef knows column computed , doesn’t try set it.

be aware of similar issue store-generated keys. default integer key "store-generated" default guid key "client generated". if, in table, database sets guid, must mark id property [databasegenerated(databasegeneratedoption.identity)]


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -

java Extracting Zip file -