EF一对一更新失败

编程入门 行业动态 更新时间:2024-10-24 17:25:13
本文介绍了EF一对一更新失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在EF6中定义了一对一的关系,该关系适用于插入.不幸的是,当我尝试使用断开记录的更新时,收到错误消息.这是详细信息:

I have one-to-one relationship defined in EF6 which works for Inserts. Unfortunately when I try an update with a disconnected record, I receive an error. Here are details:

.NET来源:

namespace EF_ConsoleApp_Test { public class Program { public static void Main(string[] args) { int? accountId; int? customerId; using (var db = new MainContext()) { var account = new Account { AccountNumber = "1234", Customer = new Customer {FirstName = "John"} }; db.Accounts.Add(account); db.SaveChanges(); accountId = account.Id; customerId = account.Customer.Id; } using (var db = new MainContext()) { // disconnected record var account = new Account() { Id = accountId, AccountNumber = "9876", Customer = new Customer() {Id = customerId} }; db.Accounts.Add(account); db.Entry(account).State = EntityState.Modified; db.Entry(account.Customer).State = EntityState.Unchanged; db.SaveChanges(); // Error occurs here } } [Serializable] [Table("CUSTOMERS")] public class Customer { [Key] [Column("CUSTOMER_ID")] public int? Id { get; set; } [Required] [Column("FIRST_NAME")] [StringLength(45)] public string FirstName { get; set; } public virtual Account Account { get; set; } public Customer() { } } [Serializable] [Table("ACCOUNTS")] public class Account { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Column("ACCOUNT_ID")] public int? Id { get; set; } [Required] [Column("ACCOUNT_NUMBER")] [Display(Name = "Account Number")] [StringLength(16)] public string AccountNumber { get; set; } public virtual Customer Customer { get; set; } /// <summary> /// Default Constructor /// </summary> public Account() { } } internal class MainContext : DbContext { internal MainContext() : base("name=ACHRE.Context") { Database.SetInitializer<MainContext>(null); } public virtual DbSet<Account> Accounts { get; set; } public virtual DbSet<Customer> Customers { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure FK modelBuilder.Entity<Customer>() .HasRequired(c => c.Account) .WithRequiredPrincipal(a => a.Customer) .Map(m => m.MapKey("CUSTOMER_ID")); base.OnModelCreating(modelBuilder); } } } }

数据库表创建语句:

CREATE TABLE [dbo].[CUSTOMERS]( [CUSTOMER_ID] [INT] IDENTITY(1,1) NOT NULL, [FIRST_NAME] [varchar](45) NOT NULL, CONSTRAINT [PK_CUSTOMERS] PRIMARY KEY CLUSTERED ( [CUSTOMER_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[ACCOUNTS]( [ACCOUNT_ID] [INT] IDENTITY(1,1) NOT NULL, [CUSTOMER_ID] [int] NOT NULL, [ACCOUNT_NUMBER] [varchar](16) NOT NULL, CONSTRAINT [PK_ACCOUNTS] PRIMARY KEY CLUSTERED ( [ACCOUNT_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] ALTER TABLE [dbo].[ACCOUNTS] WITH CHECK ADD CONSTRAINT [FK_ACCOUNTS_CUSTOMERS] FOREIGN KEY([CUSTOMER_ID]) REFERENCES [dbo].[CUSTOMERS] ([CUSTOMER_ID]) GO

错误:

"Customer_Account" AssociationSet中的关系位于 已添加"状态.给定多重性约束,相应的 "Customer_Account_Target"也必须处于已添加"状态.

A relationship from the 'Customer_Account' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Customer_Account_Target' must also in the 'Added' state.

我需要更新什么才能使其正常工作?

What do I need to update to make this work?

注释

  • 这与问题在此之前,我问过与插入内容有关的问题.插入文件已解决,但在删除外键时引入了此问题.
  • 使用EF 6.2和.NET 4.7.1.
  • This related to question I asked before that was related to Inserts. The Insert was resolved but introduced this issue when the Foreign Keys were removed.
  • Using EF 6.2 and .NET 4.7.1.
推荐答案

这是因为您要再次添加实体.您需要先通过调用db.Accounts.Attach(account);附加对象.

This is because you are adding the entity again. You need to attach the object first by calling db.Accounts.Attach(account);.

或者更好的方法是先根据Id提取内容,然后像这样修改所需的字段:

Or a better approach would be to first fetch based on Id and then modify the desired fields like this :

using (var db = new MainContext()) { var account = db.Accounts.SingleOrDefault(x => x.id == accountId); account.AccountNumber = "9876" db.SaveChanges(); }

更多推荐

EF一对一更新失败

本文发布于:2023-11-14 13:53:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!