- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我升级了一个从 ef4
开始的旧项目,但现在我将它迁移到了 ef5
。
这是旧代码:
protected void SaveEntity<T>(T entity)
{
using (DocsManagerContainer context = new DocsManagerContainer())
{
string entityType = typeof(T).ToString();
GetLogger().LogMessage("Save " + entityType + " started", LogLevel.Info);
DbTransaction transaction = null;
try
{
context.Connection.Open();
transaction = context.Connection.BeginTransaction();
context.AddObject(typeof(T).Name + "s", entity);
transaction.Commit();
context.SaveChanges();
}
catch (Exception e)
{
GetLogger().LogMessage("Save " + entityType + " thrown error :", e, LogLevel.Error);
throw e;
}
finally
{
context.Connection.Close();
transaction = null;
}
GetLogger().LogMessage("Save " + entityType + " ended", LogLevel.Info);
}
}
我已经升级了几乎所有的代码,除了:context.AddObject(typeof(T).Name + "s", entity);
,但这不再受支持。
我该如何升级它?
附注我确实想使用通用代码,而不是使用开关添加相应的对象来正确 ObjectSet
附:如果我使用 .Set().Add(entity) 则错误是:
Error 2 The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.DbContext.Set<TEntity>()' D:\work\DocsManager\trunk\DocsManagerDataMapper\EF4Factory\BaseEF4Factory.cs 64 21 DocsManagerDataMapper
最佳答案
对于 DbContext,您可以使用 context.Set<T>().Add(entity)
;
示例:context.Set<User>()
相当于context.Users
所以context.Set<User>().Add(myUser)
相当于context.Users.Add(myUser)
.
你想要更接近于此的东西:
protected void SaveEntity<T>(T entity)
where T : class
{
using (DocsManagerContainer context = new DocsManagerContainer())
{
DbTransaction transaction = null;
try
{
context.Connection.Open();
transaction = context.Connection.BeginTransaction();
context.Set<T>().Add(entity);
transaction.Commit();
context.SaveChanges();
}
finally
{
context.Connection.Close();
transaction = null;
}
}
}
关于c# - 等效于 DbContext 上的 ObjectContext.AddObject(entityName, entity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14426745/
从周日开始,我尝试用 100 种方法初始化 NSMutableArray,但没有任何效果。我尝试将其设置为等于新分配和初始化的 NSMutableArray,只需分配并自行初始化变量,我能想到的每种组
我正在尝试在循环中为我的 NSMutableDictionary 设置一些值,并将字典值每次分配给 NSMutableArray,如下所示, for(int i=0;i<=lastObj;i++){
我是 xcode/iOS 开发的 super 新手。我用 objective c 编写了一个教程应用程序,它运行得非常棒。我的下一次尝试是 swift 重写它。 一些非常奇怪的事情正在发生。每次在 x
我无法将对象添加到“easyQArray”(NSMutableArray)中。当我在本地定义数组时它确实有效,但是当我将声明移动到 .h 文件中时它不再起作用。 当我尝试访问数组中不存在的索引时,应用
我被这个奇怪的问题难住了。我将 Memo.Lines 作为 TString 传递给一个过程,并用 AddObject 填充它。但是对象没有被添加(通过 L := TURLLinkObject(AStr
我正在使用以下方法: -(void) finishQuestionnaire:(id)sender{ NSString * ans; NSString* qt = [[[questio
我正在向 NSMutableArray 添加对象“m”,如下所示: [m setObject:a forKey:@"a"]; [m setObject:b forKey:@"b"];
我对 iOS 开发真的很陌生,我正在尝试创建一个简单的表格 View ,所以我添加了一个表格 View Controller ,向其中添加了我的自定义类,现在我正在尝试添加项目。 所以我创建了一个新数
这个问题在这里已经有了答案: NSMutableArray addObjects - unrecognized-selector (1 个回答) 关闭 8 年前。 我正在向我的 NSMutableA
我正在尝试将一个对象添加到 Parse 中的一个关系,尽管代码执行时没有任何错误,但该关系没有出现在后端,因此该对象没有被保存。 PFObject *newContact = [PFObject ob
我有以下代码,它给出了代码后面的结果。由于某种原因,添加的对象有时会按应有的方式嵌入引号中,有时却不像下面的“Abtenauer”那样。谁能告诉我可能会发生什么? 相关代码: EQBreedInfo
有两个可变数组,根据一些语句控制流将对象添加到不同的可变数组。我知道我可以使用 [theMutableArray addObject: anyObject] 但我认为有点冗长。我真正需要的是 [obj
我正在使用 apache commons 包在我的 java 应用程序中创建连接池。 MAXACTIVE 值设置为 0,MINACTIVE 值设置为 1。使用 setMaxWait() 方法,我将等待
我的项目中有一组这样的图片, 我正试图将它们全部放入一个可变数组中,就像这样, NSMutableArray *imageArray = [[[NSMutableArray alloc] initWi
我正在尝试创建数组(城市)的数组(州)。每当我尝试向我的 City 数组添加一个项目时,我都会收到此错误: 'NSInvalidArgumentException', reason: '*** +[N
我一直在网上到处搜索,并尝试了迄今为止我能想到的一切,但都无济于事。这是我的问题:在我的程序中,我有一个名为 Scale 的类,Scale 有几个属性。用户可以通过单击名为 addScale 的按钮向
我假设 NSArrayController 的 addObject 和 removeObject 的工作方式与 NSMutableArray 中的类似物类似。 但是,看起来 addObject 将其目
我想知道这个方法调用的作用: stringList.addObject(String,Object); 我也想知道这个属性是做什么的: stringList.Objects[i] 添加时看起来像键、值
我的代码给出了一个错误提示 No visible interface for 'NSMutableDictionary' declares the selector 'addObjects:forKe
我有一个计算属性,它从另一个属性构建一个数组并返回它。当我用 addObject 更新这个数组时, set显然没有被调用,所以我无法更新原始属性。有没有办法让我也更新 addObject 上的原始属性
我是一名优秀的程序员,十分优秀!