如何在实体freamwork的同时更新主表和外表

时间:2017-02-20 16:53:13

标签: entity-framework-6

我有主表学生和外国表学术

public partial class StudentInfo
{
    public StudentInfo()
    {
        this.StudentCurrentAcademics = new HashSet<Academic>();
    }

    public int StudentInfoID { get; set; }
    public Nullable<int> BranchID { get; set; }
    public string ScholarID { get; set; }
    public string Religion { get; set; }



    public virtual ICollection<Academic> StudentCurrentAcademics { get; set; }
}

我的foregin表是

 public partial class Academic
{
    public decimal CurrentAcademicID { get; set; }
    public Nullable<int> StudentInfoID { get; set; }
    public Nullable<int> CourseID { get; set; }
    public Nullable<int> StandardID { get; set; }
    public virtual StudentInfo StudentInfo { get; set; }
}

我想使用

同时更新两个表

我用Academic对象初始化了StudentInfo表对象。 我在两个表中都设置了StudentInfoID。

现在使用

更新它
 _context.Entry(StudentInfoObj).State = System.Data.Entity.EntityState.Modified;
   _context.SaveChanges();

它正在更新数据库中的StudentInfo记录,但没有更新学术记录。

1 个答案:

答案 0 :(得分:0)

请完全像我一样尝试并且它有效。您的实体名称和数据库将有所不同。 SaveChanges()将充当交易,因此您的更新将“同时”发生。

SQL:

USE [Breaz]
GO
/****** Object:  Table [dbo].[Academic]    Script Date: 2/21/2017 3:18:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Academic](
    [CurrentAcedemicID] [int] IDENTITY(1,1) NOT NULL,
    [StudentInfoID] [int] NULL,
    [CourseID] [int] NULL,
    [StandardID] [int] NULL,
 CONSTRAINT [PK_Academic] PRIMARY KEY CLUSTERED 
(
    [CurrentAcedemicID] 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
/****** Object:  Table [dbo].[StudentInfo]    Script Date: 2/21/2017 3:18:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StudentInfo](
    [StudentInfoID] [int] IDENTITY(1,1) NOT NULL,
    [BranchID] [int] NULL,
    [ScholarID] [varchar](10) NOT NULL,
    [Religion] [varchar](10) NOT NULL,
 CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED 
(
    [StudentInfoID] 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
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Academic] ON 

GO
INSERT [dbo].[Academic] ([CurrentAcedemicID], [StudentInfoID], [CourseID], [StandardID]) VALUES (1, 1, 2, 3)
GO
INSERT [dbo].[Academic] ([CurrentAcedemicID], [StudentInfoID], [CourseID], [StandardID]) VALUES (2, 1, 3, 5)
GO
SET IDENTITY_INSERT [dbo].[Academic] OFF
GO
SET IDENTITY_INSERT [dbo].[StudentInfo] ON 

GO
INSERT [dbo].[StudentInfo] ([StudentInfoID], [BranchID], [ScholarID], [Religion]) VALUES (1, 1, N'1', N'na')
GO
SET IDENTITY_INSERT [dbo].[StudentInfo] OFF
GO
ALTER TABLE [dbo].[Academic]  WITH CHECK ADD  CONSTRAINT [FK_Academic_StudentInfo] FOREIGN KEY([StudentInfoID])
REFERENCES [dbo].[StudentInfo] ([StudentInfoID])
GO
ALTER TABLE [dbo].[Academic] CHECK CONSTRAINT [FK_Academic_StudentInfo]
GO

添加EDMX

这是控制器

public class HomeController : Controller
{
    public ActionResult Index23()
    {
        using (BreazEntities5 entity = new BreazEntities5())
        {
            var student = entity.StudentInfoes.Where(r => r.StudentInfoID == 1).
                Include(s => s.Academics).FirstOrDefault();
            student.Religion = "need more";
            student.Academics.ToList().ForEach(r => r.CourseID = 999);
            entity.Entry(student).State = System.Data.Entity.EntityState.Modified;
            entity.SaveChanges();
        }
        return View();
    }