简体   繁体   中英

DataObjects.NET 4.0 Save certain data

I am creating a WinForms application in C# using DataObjects.NET. I'm a little bit stuck on a certain point. I have some data in a database. It are objects as below:

[HierarchyRoot]
[Index("Id", Unique = true)]
public class Object : Entity
{
    [Field]
    public string Id { get; set; }
    [Field(Length = 100)]
    public string Value { get; set; }

    public Object(Session session)
        : base(session)
    {
    }

    public Object(Session session,string Id,string Value)
        : base(session)
    {
       //...
    }
}

Now I need the functionality to import objects out of a CSV file and compare them with the data available in the database. As my constructor takes 1 argument of Xtensive.Orm.Session , I import the objects as followed and return a list with all imported objects.

 while ((sLine = sr.ReadLine()) != null)
 {
        //Object o = new Object(Xtensive.Orm.Session,string id, string value)
        Object o = new Object(session, sLine.Split(';')[0], sLine.Split(';')[1]);

        objectlist.Add(o);
 }
 return objectlist;

After I got my imported list, I do some checks on the data and I analyze the data to fill 2 new lists with changed objects and new objects. Now my question is, if a User clicks on a save button, the new objects and changed objects have to be updated or added in the database. I tried to use Session.SaveChanges(); , but as all my objects inherit from Entity , the session wants to save all the objects resulting in SQL-errors on duplicate rows. I am not sure how to fix this problem.

 private bool SaveChanges()
 {
      foreach(Object o in newObjects)
           //Add new objects to db
      foreach(Object o in changedObjects)
           //Update object
 }

I was looking on the world wide web to find a solution, but couldn't find a well explained one.

I am new to DataObjects and I know it's an old ORM framework but I have to use it. I hope some of you guys could help me out with this annoying problem as their support is pretty dead.

As I understand, DataObjects.Net doesn't implement Unit of Work. Instead, it holds connection open and pushes updates as soon as you make changes (create/update/delete instances).

So, with

Object o = new Object(session, sLine.Split(';')[0], sLine.Split(';')[1]);

DataObjects.Net stores a record to DB corresponding to this object.

And with

private bool SaveChanges()
 {
      foreach(Object o in newObjects)
           //Add new objects to db
      foreach(Object o in changedObjects)
           //Update object
 }

you are forcing to do it again. Thus you've got this error.

So correct solution suggests 1) detect if some objects already have been stored, 2) create objects (which are really need to be created) like you do, 3) don't call anything like SaveChanges, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM