Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save a model that has existing data as well as new data?

I currently have a model that has existing data as well as new data.

As an example this is my model

 public class NameDetails
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

This is the mock data that it currently has

  List<NameDetails> Names = new List<NameDetails>{

                new  NameDetails{Id = 1, Name = "Name 1"},
                new NameDetails{Id = 2 , Name = "Name 2"},
            };

Now suppose I need to save this onto a database.. I already have the id = 1 in the table, so that should be an update where as the id = 2 should be an add... how can I do that?

Previously when I have written saves using a repository I did either an add or an edit Add like so,

 context.NameDetails.Add(NameDetails); 
    context.SaveChanges(); 

or Edit like so,

var recordToUpdate = context.NameDetails.FirstOrDefault(x => x.Id== 1);
recordToUpdate.Name = "New name";
context.SaveChanges();

so does that mean I have to loop through my list and work out what is new and what isn't.. or is there another way?

like image 389
user2206329 Avatar asked Jul 24 '13 12:07

user2206329


People also ask

How do I save a python model and use it later?

Save Your Model with pickle Pickle is the standard way of serializing objects in Python. You can use the pickle operation to serialize your machine learning algorithms and save the serialized format to a file. Later you can load this file to deserialize your model and use it to make new predictions.

How do you save a keras model?

Call tf. keras. Model. save to save a model's architecture, weights, and training configuration in a single file/folder .


2 Answers

You could use some conventions that work fine with Entity Framework.

For instance, if you use IDENTITY(1,1) on your database (so it generates IDs for your inserted rows automatically), your entity property should use StoreGeneratedPattern set as Identity (case of Model first approach), and your Id property with a 0 value means it is not yet added into the database.

Then you could easily decide what's to add and what's to update. Here's some pseudo (untested) code:

foreach (var entity in entities)
{
    if (entity.Id == 0)
    {
        // Adds to the context for a DB insert
        context.Entities.Add(entity);
    }
    else
    {
        // Updates existing entity (property by property in this example, you could update
        // all properties in a single shot if you want)
        var dbEntity = context.Entities.Single(z => z.Id == entity.Id);
        dbEntity.Prop1 = entity.Prop1;
        // etc...
    }
}

context.SaveChanges();
like image 170
ken2k Avatar answered Nov 15 '22 12:11

ken2k


EF5+ only:

If you have a way to detect whether or not an item is new (Id == 0 is good, as in ken2k's answer) you can do this to avoid needing to map things:

foreach(var entity in entities)
{
    context.NameDetails.Attach(entity);
    context.Entry(entity).State = IsNewEntity(entity) 
                                  ? EntityState.Added
                                  : EntityState.Modified;
}

This will tell EntityFramework to create INSERTs for the new entities and UPDATEs for the old entities.

like image 35
Steve Ruble Avatar answered Nov 15 '22 13:11

Steve Ruble