C# 添加或更新记录?
我有一个MVC应用程序,在控制器的POST方法中有以下代码。我正在做EF添加,显然这是不对的。我希望它添加记录,如果它不存在,否则更新。请问我怎么做C# 添加或更新记录?,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个MVC应用程序,在控制器的POST方法中有以下代码。我正在做EF添加,显然这是不对的。我希望它添加记录,如果它不存在,否则更新。请问我怎么做 try { AttributeEntities db = new AttributeEntities(); IEnumerable<string> items = viewModel.SelectedAttributes2; int i = 0; foreach (var item in items)
try
{
AttributeEntities db = new AttributeEntities();
IEnumerable<string> items = viewModel.SelectedAttributes2;
int i = 0;
foreach (var item in items)
{
var temp = item;
// Save it
SelectedHarmonyAttribute attribute = new SelectedHarmonyAttribute();
attribute.CustomLabel = viewModel.ItemCaptionText;
attribute.IsVisible = viewModel.Isselected;
string harmonyAttributeID = item.Substring(1, 1);
// attribute.OrderNumber = Convert.ToInt32(order);
attribute.OrderNumber = i++;
attribute.HarmonyAttribute_ID = Convert.ToInt32(harmonyAttributeID);
db.SelectedHarmonyAttributes.Add(attribute);
db.SaveChanges();
}
}
试试看
{
AttributeEntities db=新的AttributeEntities();
IEnumerable items=viewModel.selectedAttribute 2;
int i=0;
foreach(项目中的var项目)
{
var temp=项目;
//省省吧
SelectedHarmonyAttribute=新建SelectedHarmonyAttribute();
attribute.CustomLabel=viewModel.ItemCaptionText;
attribute.IsVisible=viewModel.Isselected;
字符串harmonyAttributeID=item.Substring(1,1);
//attribute.OrderNumber=Convert.ToInt32(订单);
attribute.OrderNumber=i++;
attribute.HarmonyAttribute_ID=Convert.ToInt32(harmonyAttributeID);
db.SelectedHarmonyAttributes.Add(属性);
db.SaveChanges();
}
}
您需要检查数据库中您试图添加/更新的记录。如果查找返回null,则表示数据库中不存在该值。如果有,您可以修改查找的记录,并调用db.SaveChanges()来保存对数据库所做的更改
编辑:
int id = Convert.ToInt32(harmonyAttributeID);
var existingEntry = db.SelectedHarmonyAttributes.SingleOrDefault(x => x.HarmonyAttribute_ID == id);
您可以导入System.Data.Entity.Migrations命名空间并使用AddOrUpdate扩展方法:
db.SelectedHarmonyAttributes.AddOrUpdate(attribute);
db.SaveChanges();
编辑:
我假设SelectedHarmonyAttributes是DbSet类型
编辑2:
这样做的唯一缺点(这可能不是您的问题)是您的实体不对自己的状态更改负责。这意味着您可以将实体的任何属性更新为无效的内容,您可能需要在实体本身上对其进行内部验证,或者可能需要在更新时执行一些您始终希望执行的其他处理。如果您关心这些问题,那么应该在实体上添加公共更新方法,并首先检查其在数据库中的存在性。e、 g:
var attribute = db.SelectedHarmonyAttributes.SingleOrDefault(x => x.HarmonyAttribute_ID == harmonyAttributeID);
if (attribute != null)
{
attribute.Update(viewModel.ItemCaptionText, viewModel.Isselected, i++);
}
else
{
attribute = new Attribute(viewModel.ItemCaptionText, viewModel.Isselected);
db.SelectedHarmonyAttributes.Add(attribute);
}
db.SaveChanges();
您的更新方法可能类似于:
public void Update(string customLabel, bool isVisible, int orderNumber)
{
if (!MyValidationMethod())
{
throw new MyCustomException();
}
CustomLabel = customLabel;
IsVisible = isVisible;
OrderNumber = orderNumber;
PerformMyAdditionalProcessingThatIAlwaysWantToHappen();
}
然后将所有实体的属性都设置为公共的“get”但受保护的“set”,这样它们就不能从实体本身外部进行更新。这可能有点偏离正切,但使用AddOrUpdate方法会假定您不想控制更新发生的方式,并保护您的域实体不进入无效状态等。希望这有帮助 确定添加或更新的一种常见方法是查看标识符字段并设置适当的状态
using System.Data;
SelectedHarmonyAttribute attribute;
using (var db = new YourDbContext())
{
db.Entry(attribute).State = attribute.HarmonyAttribute_ID == 0 ? EntityState.Added : EntityState.Modified;
db.SaveChanges();
}
您需要存储要保存的属性的ID,检查它是否存在于数据库中。如果有,请更新其属性并保存更改。如果没有,那么创建并添加它。检查这篇文章的答案。[这里][1][1]:你能用密码告诉我吗?如何查找?请小心。这不是它的设计目的……是的,我在发布答案后意识到它确实有缺点。我已经对我的答案进行了编辑,以进一步限定它,但是如果理解了缺点并且知道了要比较的主键,那么它可能是合适的。(在本例中,HarmonyAttribute_ID似乎是主键)