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.