Solved

.edmx and ADD DEFAULT

Posted on 2010-09-14
18
354 Views
Last Modified: 2013-11-11
Hi experts, I am using linq entity framework for recording data, but I have a problem with the default values
ALTER TABLE [certified]. [Activity] ADD DEFAULT ('1 ') FOR [SituacionRegistro]
my question is, as I work on .edmx to take the default

in my column model .edmx  is not SituacionRegistro
0
Comment
Question by:enrique_aeo
  • 9
  • 9
18 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33674455
Not sure of your question. Are you try to update/insert into the database a default value or are you trying to read from the database a null value and trying to assign a default into the model?
0
 

Author Comment

by:enrique_aeo
ID: 33674487
Hi, I'm trying to insert a record and the default value of SituacionRegistro column in the database is not working, always insert null value
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33674582
Please post your code.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:enrique_aeo
ID: 33674720
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33675878
The only thing that I can see is that you are defining this in the database

ALTER TABLE [certified]. [Activity] ADD DEFAULT ('1 ') FOR [SituacionRegistro]

the default is being set to '1 ' where the max chars is 1 try changing '1 ' to '1'
0
 

Author Comment

by:enrique_aeo
ID: 33675991
the code in the database is well
ALTER TABLE [certifica].[Actividad] ADD  DEFAULT ('1') FOR [SituacionRegistro]

I tried entering data with transact sql (insert into) and runs the default.

I assume that the error is in the LINQ
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33676954
Are you sending the field SituacionRegistro in the insert?
0
 

Author Comment

by:enrique_aeo
ID: 33677003
he who does the recording is the same model with this method
 public Actividad CrearActividad(Actividad ActividadACrear)
        {
            db.AddToActividad(ActividadACrear);
            Grabar();
            return ActividadACrear;
        }

when I call this method, do not send SituacionRegistro
 actividad = new Actividad();
                    actividad.CodigoActividad = codigoActividad;
                    actividad.NombreActividad = txtNombreCurso.Text.Trim();
                    actividad.NombreSede = txtNombreSede.Text.Trim();
                    actividad.FechaInicio = txtFechaInicio.Text.Trim();
                    actividad.FechaFin = txtFechaFin.Text.Trim();
                    actividad.DirigidoA = txtDirigido.Text.Trim();
                    actividad.NombrePrograma = txtNombrePrograma.Text.Trim();
                    actividad.NumeroResolucion = txtNumeroResolucion.Text.Trim();
                    actividad.HorasLectivas = txtHorasLectivas.Text.Trim();
                    actividad.UsuarioCreacion = Session["userConnected"].ToString();
                    actividad.FechaCreacion = DateTime.Now;
                    repositorio.CrearActividad(actividad);
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33677113
Try setting this in the above code.

actividad.SituacionRegistro = null;
0
 

Author Comment

by:enrique_aeo
ID: 33677411
I made the change, but the problem persists, ie does not insert the default value is defined in the database
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33677535
Do you have a SQL Profiler to see what SQL query is being sent to the server?
0
 

Author Comment

by:enrique_aeo
ID: 33677827
NULL arrives, VIEW ATTACHED
traceSituacionRegistro.jpg
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33683041
Hi enrique_aeo;

In doing some research on the net I have found that Default Values assigned at the server are not supported in EF. I have test this and it seems to be true on VS 2010 as well. The only way I have found to get around this is to set the EF Default Value for that column in the model. To do this you can do the following:

1. Open the EF designer.
2. On the table that you want to assign default values to, right click on the column name and select
     Properties from the context menu.
3. In the Properties window select the Default Value property and set the value in the text box to the right
    to the default value you wanted for that column.

That should do it.

Fernando
0
 

Author Comment

by:enrique_aeo
ID: 33683349
I made the change in 2 ways
1. property change in the model and kept the default value in the database
2. property change in the model and delete the default value in the database
In no case does
situacionRegistroEDM.jpg
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33683547
I did my test using VS 2010, what version are you running?
0
 

Author Comment

by:enrique_aeo
ID: 33683561
vs 2008
this would be the problem?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 33684120
You may want to look at the ObjectContext.SavingChanges Event and check all the values that have default values and set them here.

ObjectContext.SavingChanges Event
http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.savingchanges.aspx

It is about all I can think of doing at this point.
0
 

Author Closing Comment

by:enrique_aeo
ID: 33684155
Thank you very much, hope can help with another problem (ID: 26473224 - LINQ-The data reader is incompatible with)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 32
What can cause the styling on a .NET site to not be found? 17 25
ASP.NET MVC identity 6 26
VB.NET 2008 - SQL Timeout 9 24
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question