postgres, database-first model, string fields with NOT NULL DEFAULT ''
postgres, database-first model, string fields with NOT NULL DEFAULT ''
Hi,
[Intro - I am making the transition from winforms to asp.net mvc, skipping regular asp.net. I think I get the principles of the design pattern and I have been trying my first sample project. I do have to say that a tutorial for a sample postgres, asp.mvc5, EF6 ,database first model would be a real help.Also, do find it quite annoying that you don't get a reminder that support is going to expire. i have gone and renewed my licence in order to be able to post this]
I have created basic project, added the devart section into providers in web.config, created a basic ADO.NET Entity Data Model and using database-first have created a model
my problem seems to be with text fields(character, character varying etc.) where the field is defined as NOT NULL DEFAULT ''. I am sure that some might suggest this is not the best practice and test fields are best left as DEFAULT NULL, but i have lot of legacy data and need to work with this.
When i 'create new' and specify a value for the string field, all works well. my problem comes when i try to leave the string field blank. I have added a try/catch block around the savechanges call and it gives me
Entity of type "test" in state "Added" has the following validation errors:
- Property: "tst_string", Value: "", Error: "The tst_string field is required."
I am confused, has a value of empty string but validation is saying it is required
I have ready to send a sample, surprised i cant add one directly to the ticket
Note, i now the pk/identity field is not configured properly, that is what i intend to understand next. at the moment i am just manually entering a value i know is unique.
Also, i have chosen to use ADO.NET model as opposed to Entity Developer, i can't get entity developer model to work with add new scaffolded item. that will probably be a separate ticket. just pointing out that i have not been able to try the entity developer to see if it has the same issue
many thanks
Nick
[Intro - I am making the transition from winforms to asp.net mvc, skipping regular asp.net. I think I get the principles of the design pattern and I have been trying my first sample project. I do have to say that a tutorial for a sample postgres, asp.mvc5, EF6 ,database first model would be a real help.Also, do find it quite annoying that you don't get a reminder that support is going to expire. i have gone and renewed my licence in order to be able to post this]
I have created basic project, added the devart section into providers in web.config, created a basic ADO.NET Entity Data Model and using database-first have created a model
my problem seems to be with text fields(character, character varying etc.) where the field is defined as NOT NULL DEFAULT ''. I am sure that some might suggest this is not the best practice and test fields are best left as DEFAULT NULL, but i have lot of legacy data and need to work with this.
When i 'create new' and specify a value for the string field, all works well. my problem comes when i try to leave the string field blank. I have added a try/catch block around the savechanges call and it gives me
Entity of type "test" in state "Added" has the following validation errors:
- Property: "tst_string", Value: "", Error: "The tst_string field is required."
I am confused, has a value of empty string but validation is saying it is required
I have ready to send a sample, surprised i cant add one directly to the ticket
Note, i now the pk/identity field is not configured properly, that is what i intend to understand next. at the moment i am just manually entering a value i know is unique.
Also, i have chosen to use ADO.NET model as opposed to Entity Developer, i can't get entity developer model to work with add new scaffolded item. that will probably be a separate ticket. just pointing out that i have not been able to try the entity developer to see if it has the same issue
many thanks
Nick
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
MVC: http://www.devart.com/dotconnect/postgr ... cific.htmlnhill383 wrote:I do have to say that a tutorial for a sample postgres, asp.mvc5, EF6 ,database first model would be a real help.
EF6: http://blog.devart.com/entity-framework ... force.html
Database-First (EDM Wizard): http://www.devart.com/dotconnect/postgr ... al_EF.html
Please send us your test project via contact form: http://www.devart.com/company/contactform.html. Our mail server receives attachments up to 2 MB. If your project is bigger, please upload it to any file exchange server and send us a download link.nhill383 wrote:When i 'create new' and specify a value for the string field, all works well. my problem comes when i try to leave the string field blank. I have added a try/catch block around the savechanges call and it gives me
Entity of type "test" in state "Added" has the following validation errors:
- Property: "tst_string", Value: "", Error: "The tst_string field is required."
I am confused, has a value of empty string but validation is saying it is required
I have ready to send a sample, surprised i cant add one directly to the ticket
Tell us the following information:nhill383 wrote:Also, i have chosen to use ADO.NET model as opposed to Entity Developer, i can't get entity developer model to work with add new scaffolded item.
1) the full text of the error you are getting with *.edml
2) the exact steps we should follow to reproduce the problem
3) the version of your Visual Studio (Help > About)
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
Hi,
Thanks for the links, i will certainly check those out.
I hope i have provided enough info below, please let me know if i can do anything else to clarify the problem
windows 7 64bit,
Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51209
dotconnect postgresql 7.3.389
DDL SQL, very basic test table
CREATE TABLE test (
tst_pk integer NOT NULL,
tst_date date,
tst_time time without time zone,
tst_string text DEFAULT ''::text NOT NULL
);
ALTER TABLE ONLY test
ADD CONSTRAINT testtable_pkey PRIMARY KEY (tst_pk);
i navigate to ../tests/create
at the moment it is not providing value from a sequence or serial, so i am just entering a pk value i know to be unique
if i enter values fro all 4 fields then it works as expected
if i omit a value for tst_string then i get the following exception
{"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."}
with a stack trace of
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at WebApplication3.Controllers.testsController.Create(test test) in C:\Users\nhill\Documents\Visual Studio 2013\Projects\webapplication3\WebApplication3\Controllers\testsController.vb:line 57
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
for some reason i am not able to expand EntityValidationErrors and get any useful detail from it, so i added some additional code which indicated that the validation error in question was
Entity of type "test" in state "Added" has the following validation errors:
- Property: "tst_string", Value: "", Error: "The tst_string field is required."
If we are OK having two issues in the same ticket then the sample that i have sent over also include a devart entity model that i created for the same table.
I have created the model, then rebuilt the solution
then under controllers i select add new scaffolded item, MVC 5 controller with views using Entity framework and select
model class: Test (WebApplication3.DevartTestdbModel)
data contact class : DevartTestdbEntities (WebApplication3.DevartTestdbModel)
then i get the error
There was an error running the selected code generator:
'Object reference not set to an instance of an object.'
I dont know how to get any further info on the error in question
many thanks
Nick
Thanks for the links, i will certainly check those out.
I hope i have provided enough info below, please let me know if i can do anything else to clarify the problem
windows 7 64bit,
Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51209
dotconnect postgresql 7.3.389
DDL SQL, very basic test table
CREATE TABLE test (
tst_pk integer NOT NULL,
tst_date date,
tst_time time without time zone,
tst_string text DEFAULT ''::text NOT NULL
);
ALTER TABLE ONLY test
ADD CONSTRAINT testtable_pkey PRIMARY KEY (tst_pk);
i navigate to ../tests/create
at the moment it is not providing value from a sequence or serial, so i am just entering a pk value i know to be unique
if i enter values fro all 4 fields then it works as expected
if i omit a value for tst_string then i get the following exception
{"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."}
with a stack trace of
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at WebApplication3.Controllers.testsController.Create(test test) in C:\Users\nhill\Documents\Visual Studio 2013\Projects\webapplication3\WebApplication3\Controllers\testsController.vb:line 57
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
for some reason i am not able to expand EntityValidationErrors and get any useful detail from it, so i added some additional code which indicated that the validation error in question was
Entity of type "test" in state "Added" has the following validation errors:
- Property: "tst_string", Value: "", Error: "The tst_string field is required."
If we are OK having two issues in the same ticket then the sample that i have sent over also include a devart entity model that i created for the same table.
I have created the model, then rebuilt the solution
then under controllers i select add new scaffolded item, MVC 5 controller with views using Entity framework and select
model class: Test (WebApplication3.DevartTestdbModel)
data contact class : DevartTestdbEntities (WebApplication3.DevartTestdbModel)
then i get the error
There was an error running the selected code generator:
'Object reference not set to an instance of an object.'
I dont know how to get any further info on the error in question
many thanks
Nick
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
You can access EntityValidationErrors either in the code (link) or in the Watch window of Visual Studio (link).nhill383 wrote:for some reason i am not able to expand EntityValidationErrors and get any useful detail from it
A solution is described at http://forums.devart.com/viewtopic.php? ... 23#p104619. Implement the needed changed in your *.edml model, rebuild the project, and try adding controller again.nhill383 wrote:If we are OK having two issues in the same ticket then the sample that i have sent over also include a devart entity model that i created for the same table.
I have created the model, then rebuilt the solution
then under controllers i select add new scaffolded item, MVC 5 controller with views using Entity framework and select
model class: Test (WebApplication3.DevartTestdbModel)
data contact class : DevartTestdbEntities (WebApplication3.DevartTestdbModel)
then i get the error
There was an error running the selected code generator:
'Object reference not set to an instance of an object.'
I dont know how to get any further info on the error in question
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
Hi,
Many thanks for the help.
I think i may have created some confusion, i think i have actually put three issues in the same ticket, my apologies.
1) savechanges was failing for a string field that is defined as NOT NULL DEFAULT ''. When creating a new record, it was working if you entered a string but would not work if you left it blank. I tried to describe this issue in my previous post but i don't know if you saw it as i didn't get any response to that specific question.Eventually i found a solution using <DisplayFormat(ConvertEmptyStringToNull:=False)> and adding to a metadata class and defining a partial class with <MetadataType(GetType(testMetadata))>. Is this the recommended solution? It works for ADO.NET entity model and now that i am able to scaffold a devart entity model (see item 3), it seems to work for that too. Is there a better way of doing this for devart model, it seems like it should be able to derive this from that database and do it automatically, failing that can it be done in entity developer as opposed to manually writing metadata classes.
2) EntityValidationErrors - i had done a lot of searching on this and the method i implemented is roughly similar to the one for teh code link, although that one is slightly more elegant. In the watch window i tried
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors but in vb.net i get the error "character is not valid"
if i try directcast(($exception),System.Data.Entity.Validation.DbEntityValidationException).EntityValidationErrors then i also get "Character is not valid."
but if i just try $exception then it does evaluate, i just have to click expand a few more properties to get to the validationerrors
3) perfect! once i use dbcontext template then i am able to use scaffolding OK. I am still earning but seems to make sense.
In summary,
1) it is working, would like to know if it can be improved for devart entity model
2) either code or watch approach is working, would be nice to understand whether the c# code can be converted to vb
3) devart entity model working fine so far
many thanks
Nick
Many thanks for the help.
I think i may have created some confusion, i think i have actually put three issues in the same ticket, my apologies.
1) savechanges was failing for a string field that is defined as NOT NULL DEFAULT ''. When creating a new record, it was working if you entered a string but would not work if you left it blank. I tried to describe this issue in my previous post but i don't know if you saw it as i didn't get any response to that specific question.Eventually i found a solution using <DisplayFormat(ConvertEmptyStringToNull:=False)> and adding to a metadata class and defining a partial class with <MetadataType(GetType(testMetadata))>. Is this the recommended solution? It works for ADO.NET entity model and now that i am able to scaffold a devart entity model (see item 3), it seems to work for that too. Is there a better way of doing this for devart model, it seems like it should be able to derive this from that database and do it automatically, failing that can it be done in entity developer as opposed to manually writing metadata classes.
2) EntityValidationErrors - i had done a lot of searching on this and the method i implemented is roughly similar to the one for teh code link, although that one is slightly more elegant. In the watch window i tried
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors but in vb.net i get the error "character is not valid"
if i try directcast(($exception),System.Data.Entity.Validation.DbEntityValidationException).EntityValidationErrors then i also get "Character is not valid."
but if i just try $exception then it does evaluate, i just have to click expand a few more properties to get to the validationerrors
3) perfect! once i use dbcontext template then i am able to use scaffolding OK. I am still earning but seems to make sense.
In summary,
1) it is working, would like to know if it can be improved for devart entity model
2) either code or watch approach is working, would be nice to understand whether the c# code can be converted to vb
3) devart entity model working fine so far
many thanks
Nick
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
a small update to point 2)
if i expand $exception as far as validationerrors and then right-click add watch against that then it adds
(New System.Collections.Generic.Mscorlib_CollectionDebugView(Of System.Data.Entity.Validation.DbEntityValidationResult)($exception.EntityValidationErrors)).Items(0).ValidationErrors
but i still get "Character is not valid."
thanks
if i expand $exception as far as validationerrors and then right-click add watch against that then it adds
(New System.Collections.Generic.Mscorlib_CollectionDebugView(Of System.Data.Entity.Validation.DbEntityValidationResult)($exception.EntityValidationErrors)).Items(0).ValidationErrors
but i still get "Character is not valid."
thanks
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
Your current approach with defining attributes in a partial class works, doesn't it? As an alternative solution with Entity Developer, you can set attributes via interface of designer: navigate to Model > Settings > Attributes > select the assembly (e.g.: System.ComponentModel.DataAnnotations.dll) with the needed attributes and make sure that the required attributes (e.g.: DisplayFormatAttribute) are checked in the window below, press OK. After this, select the needed attribute in the Attributes collection of a particular class or property and initialize its options (if any).nhill383 wrote:1) it is working, would like to know if it can be improved for devart entity model
Refer to this link.nhill383 wrote:2) either code or watch approach is working, would be nice to understand whether the c# code can be converted to vb
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
Hi,
so I spent some time trying to get attributes to work in the model. I was actually putting together a sample to submit a new ticket. Then i decided to search the forums and found a suggestion. In the template i have set Validation Framework to Data Annotations, is that right?
It is not obvious at first as some of the data annotations are not related to validation. Am I right that in order for any of the attributes in the model to be applied then Validation Framework needs to be set to Data Annotations
thanks
so I spent some time trying to get attributes to work in the model. I was actually putting together a sample to submit a new ticket. Then i decided to search the forums and found a suggestion. In the template i have set Validation Framework to Data Annotations, is that right?
It is not obvious at first as some of the data annotations are not related to validation. Am I right that in order for any of the attributes in the model to be applied then Validation Framework needs to be set to Data Annotations
thanks
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
There are two ways to add attributes via interface of Entity Developer:
1. Validation Framework
a) open Tools > Entity Developer > Model Explorer > the Templates node, select your template, navigate to its properties:
-> set the Validation Framework property (e.g., to "Data Annotations")
-> open Validation Error Messages and type the text of messages
b) set Validate Required=True for a particular class property
2. Custom Attributes (described in my previous message).
The [1] approach is easier to use. If [1] doesn't cover your needs, you can add any attributes in the way which is described in [2].
1. Validation Framework
a) open Tools > Entity Developer > Model Explorer > the Templates node, select your template, navigate to its properties:
-> set the Validation Framework property (e.g., to "Data Annotations")
-> open Validation Error Messages and type the text of messages
b) set Validate Required=True for a particular class property
2. Custom Attributes (described in my previous message).
The [1] approach is easier to use. If [1] doesn't cover your needs, you can add any attributes in the way which is described in [2].
Re: postgres, database-first model, string fields with NOT NULL DEFAULT ''
Hi
Thanks, i am still struggling with trying to apply atributes directly within the model, as opposed to using partial class and metadatatype.
I am going to create a new ticket as it feels like it has moved quite a way from the original question that was posed here.
thanks
Thanks, i am still struggling with trying to apply atributes directly within the model, as opposed to using partial class and metadatatype.
I am going to create a new ticket as it feels like it has moved quite a way from the original question that was posed here.
thanks