我希望将记录添加到我的数据库中,但不确定最佳方法。
以下是我的模特:
public class fList
{
public int fListID { get; set; }
public string Title { get; set; }
public int UserID { get; set; }
public ICollection<Item> Items { get; set; }
}
public class Item
{
public int ItemID { get; set; }
public string Name { get; set; }
public ICollection<fList> fLists { get; set; }
}
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public ICollection<fList> fLists { get; set; }
}
执行操作之前,这是数据库:
以下是执行操作后数据库的外观:
我想要做的是添加新的fList
和新的User
。我还想添加新的Items
,但仅限于它们尚未存在的地方。我希望相应地Items
加入fList
。
我希望通过最少的db调用尽可能高效地实现上述目标。
到目前为止,这是控制器:
var flist = new fList
{
Title = "Colors",
UserName = "Chris"
Items = new List<Item>()
};
flist.Items.Add(new Item { Name = "White" });
flist.Items.Add(new Item { Name = "Orange" });
flist.Items.Add(new Item { Name = "Purple" });
flist.Items.Add(new Item { Name = "Blue" });
flist.Items.Add(new Item { Name = "Green" });
foreach (var item in flist.Items)
{
// Check item exists in database
var existingitem = db.Items.SingleOrDefault(n => n.Name == item.Name);
// If item doesn't exist, add it to database
if (existingitem == null) {
db.Items.Add(item);
}
// ...What next?
}
db.fLists.Add(flist);
db.SaveChanges();
答案 0 :(得分:0)
如果您有时间,可以从Microsoft下载Prodinner示例,这是一个完美的解决方案,包括OOP,DI,IOC等。它还向您展示了保存数据的通用功能。
如果您需要在保存记录期间控制状态,则需要实现自己的状态控制。创建接口,枚举和实现类,如下所示。
public interface IDataObjectWithState
{
State State { get; set; }
}
public enum State
{
Added,
Unchanged,
Modified,
Deleted
}
public abstract class DataObjectWithState : IDataObjectWithState
{
[NotMapped]
public State State { get; set; }
}
After that any model class you want control the state you need inherit this Class, so your models have extra UnMap State columns.
In your DB Context file, make a contractor as sample below.
public Db()
: base("name=TP_HOST")
{
try
{
((IObjectContextAdapter)this).ObjectContext
.ObjectMaterialized += (sender, args) =>
{
var entity = args.Entity as IDataObjectWithState;
if (entity != null)
{
entity.State = YourEnumNamespace.State.Unchanged;
}
};
}
catch (Exception ex)
{
}
finally { }
}
// this class need include using System.Data.Entity.Infrastructure;
public static EntityState ConvertState(YourEnumNamespace.State state)
{
switch (state)
{
case YourEnumNamespace.State.Added:
return EntityState.Added;
case YourEnumNamespace.State.Deleted:
return EntityState.Deleted;
case YourEnumNamespace.State.Modified:
return EntityState.Modified;
default:
return EntityState.Unchanged;
}
}
Then before you make the call of db.SaveChanges(), you have to set the state like sample below
foreach (var entry in dbContext.ChangeTracker
.Entries<IDataObjectWithState>())
{
IDataObjectWithState stateInfo = entry.Entity;
entry.State =Functions.ConvertState(stateInfo.State);
}
I know difficult to understand mine code, what I want to mention is we define our own enum state, we tell the models what we want (Add, Unchanged, Update, Delete), using for loop change the state become knowing by System.Data.Entity.Infrastructure.