locked
Entity Framework - optional: default value from database RRS feed

  • Question

  • User-401080925 posted

    Hi

    I need for the same insert to use or not default values from database.

    In ADO.NET I could do that excluding fields on insert,now using EF6 with Database First and I need the same,

    but Setting StoreGeneratedPattern to Computed

     takes default value all the time,but are cases for the same field when I want to insert a values and not the default,

    it is possible there?

    I've tried also something like this with no success:

    Person p = new Person () { FIELD1=val1,FIELD5=val5};
    db.Person.Add(m);

    thanks

    Monday, August 14, 2017 6:14 PM

All replies

  • User-359936451 posted

    It looks like you are adding a table to the database instead of adding a record to the existing table.

    show more of your code.

    Monday, August 14, 2017 6:35 PM
  • User753101303 posted

    Hi,

    Good question. Never tried and a quick search doesn't seems to find much.

    I see that my never really questioned approach with EF is to use a SQL Server side default value if it is always the same and to always generate them client side if not (depending on your use case, you could still initialize a property with a default value so that you don't have always to think about it and then assign another value if you want).

    Maybe my marking a property as not being modified but I'm not sure what it gives an insert and IMO it might make things worse than handling them exclusively on the server or on the client side.

    Monday, August 14, 2017 7:01 PM
  • User-401080925 posted

    Hi.I'm not adding a table,maybe variable name was confusing,I've edited my post above.thanks

    Monday, August 14, 2017 7:48 PM
  • User-359936451 posted

    Don't exclude the values on INSERT, put something there. Either a blank for strings, or 0 for int. But you will need to handle that in your code, otherwise you will get the default DB value. Which could also be Null, or blank, depending on how you configure it.

    Monday, August 14, 2017 8:09 PM
  • User-401080925 posted

    Hi,only in ADO when excluding fields on insert got default values,in EF if excluding always default value is NULL,not default set on db

    Tuesday, August 15, 2017 5:39 AM
  • User-1508012637 posted

    Hi moris27,

    but Setting StoreGeneratedPattern to Computed

     takes default value all the time,but are cases for the same field when I want to insert a values and not the default,

    it is possible there?

    According to your description, if you want the entity’s property to have a default value, I suggest you could do that in the entity class’s constructor. Please refer to the following code:

    public class Person
    {
        public Person()
        {
            FIELD3 = "Default"; //set default value here
        }
    
        public int Id { get; set; }
    
        public string FIELD1 { get; set; }
    
        public string FIELD2 { get; set; }
    
        public string FIELD3 { get; set; }
    }
    

    And FIELD3 would be “default” if you don’t assign value to the property:

    Person p1 = new Person { FIELD1 = "val1", FIELD2 = "val2" };
    Person p2 = new Person { FIELD1 = "val1", FIELD2 = "val2", FIELD3 = "val3" };
    
    db.Persons.Add(p1);
    db.Persons.Add(p2);
    db.SaveChanges();
    

    The records in database would be:

    Id            FIELD1   FIELD2   FIELD3

    1              val1        val2        Default

    2              val1        val2        val3

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Tuesday, August 15, 2017 6:46 AM
  • User-401080925 posted

    Thanks for your answer,but i'm using Db First as I've said in first post.thanks

    Tuesday, August 15, 2017 8:50 AM
  • User-1508012637 posted

    Hi moris27,

    Thanks for your answer,but i'm using Db First as I've said in first post.thanks

    It doesn’t matter whether you use Database First or Code First. The entity objects are created before they are mapped to the database by Entity Framework. So you could just add a constructor to the Person class which is generated by Entity Framework within the edmx file.

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Tuesday, August 15, 2017 9:33 AM
  • User-401080925 posted

    class is not rewritten when I update model from db?

    Tuesday, August 15, 2017 10:44 AM
  • User-1508012637 posted

    Hi moris27,

    class is not rewritten when I update model from db?

    If you update the model from database and choose “refresh”, the constructor would be removed from the current model. But it doesn’t matter, you could add the constructor again and there’s no conflict between the modified model and the table in database.

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Wednesday, August 16, 2017 1:49 AM
  • User-401080925 posted

    In this case I better put default values when inserting data.Thanks for your answers

    Wednesday, August 16, 2017 5:34 AM
  • User753101303 posted

    Or see https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

    You can split the source code for a single class into multiple files. This way you are able to refresh generated code and keep your own additional code in a separate file.

    Wednesday, August 16, 2017 8:44 AM
  • User-401080925 posted

    Seems better.thanks

    Wednesday, August 16, 2017 10:11 AM