Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server EF4.0
Hi experts
 
I got a table in SQL Server 2008 has 10 columns and one of them is a computet column
(([FirstName]+n' ')+[LastName])
 
But when I try to insert a new object to the table with entityframework 4, it can't and an exception like computed column can not be null
 
But if I remove the column form the table it can be inserted.
 
I use .NETFramework 4 and Visual Stadio 2010
 
What can I do to solve this problem it's necessary to have computed column and EF4 doesn't work for this.
I don't want to use SP (Stored Procedure) too?
Posted 5-May-12 1:47am
Edited 5-May-12 11:24am
v3
Comments
Wes Aday at 5-May-12 8:07am
   
If you have a constraint on the computed column that is cannot be null, remove the constraint.
Vartan Khachatourian at 5-May-12 8:45am
   
it's not null because firstname and lastname are not null
Wes Aday at 5-May-12 8:47am
   
What exactly does that have to do with a constraint on the database column?
Vartan Khachatourian at 5-May-12 8:51am
   
there is not any constraint on this column
Wes Aday at 5-May-12 8:52am
   
Well that is where you are getting that error.
Vartan Khachatourian at 5-May-12 8:53am
   
well whats your solution?
Zoltán Zörgő at 5-May-12 17:04pm
   
Are you using
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
attribute?
Vartan Khachatourian at 5-May-12 17:25pm
   
no but if i must use it , where am i supposed to add the arrtibute?
Zoltán Zörgő at 5-May-12 17:49pm
   
Well this is a DataAnnotation attribute. You should put it where your other attibutes are. Are you following database-first or code-firts approach? I suppose the first one. In this case it is a little bit difficult to add attributes: you can use partial classes. Here you have an example: http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
Zoltán Zörgő at 6-May-12 0:34am
   
Another: you can use a read-only not mapped property in EF instead of calculate in on sql side. You will probalby gain performance too.
Vartan Khachatourian at 6-May-12 6:01am
   
thank you i try it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 5 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100