在实体框架中映射多对多关系

发布于 2024-10-18 22:42:32 字数 912 浏览 1 评论 0原文

我有一个 sql server 数据库和 ac# win forms 应用程序,并且我正在使用实体框架。

在数据库中,我有以下表格:

Table Joint(
      JointID
      FirstName
      FatherName
      LastName)

代表健身房的一个关节 每个关节都有一个衣柜,每个关节有一个衣柜,每个衣柜可以被一个关节租用。 所以我认为这是一对一的关系,我不知道这是否属实,但无论如何,这是壁橱桌子:

Table Closet(
   ClosetID
   Number)

但同时我想保留每个壁橱的历史记录,因为也许有人租它 2 个月,然后其他人租第一个之后的同一个壁橱,所以我想知道每个联合租用这个壁橱,我想知道谁是自己认识的以及他为壁橱的租金支付了多少钱,所以我想要一个开始时间,结束时间,价格。

最后我发现我想要在上面的表之间有第三个表,它将是多对多的关系,所以我创建了:

Table ClosetHistory(
   JointID     fk
   ClosetID    fk
   StartTime
   EndTime
   Price)

我认为这是正确的,但我不确定,这都不是我的问题。 我的问题是,当我从数据库更新我的实体时,未创建关节和壁橱之间的关系,它创建了一个独立的关节实体和壁橱实体,但它不包含开始时间、结束时间、价格属性,并且未创建壁橱历史记录这是真的,因为多对多关系中的两个表之间的表不表示为实体,但是如果我从 ClosetHistory 表中删除开始日期和结束日期、价格字段并重新更新我的实体,它就会工作并创建关系 但我无法保存壁橱的开始时间和结束时间以及价格,

你能帮我吗?

我对所有这些解释感到抱歉,我对我的英语感到抱歉,我知道这非常糟糕:)

I have a sql server database and a c# win forms application and I am using entity framework.

In the database I have the following tables :

Table Joint(
      JointID
      FirstName
      FatherName
      LastName)

Which represent a joint in gym
and every joint has a closet, and every joint has one closet and each closet can be rent by one joint.
so i thought this one to one relation i don't know if it's true but anyway this the closet table :

Table Closet(
   ClosetID
   Number)

but at the same time i want to keep a history for each closet because maybe someone rent it for 2 months and then someone else rent the same closet after the first one so i want to know each joint rent this closet, and i want to know who is own know and how much he payed for the rent of the closet, so i want a start time, end time, price.

at last i figured out that i want a third table between the above tables and it will be a many to many relation so i created :

Table ClosetHistory(
   JointID     fk
   ClosetID    fk
   StartTime
   EndTime
   Price)

and i think this right but i am not sure and this all not my problem.
My problem is when i update my entities from database the relation between joint and closet not created and it create a joint entity stand alone and a closet entity but it doesn't contain a startTime, EndTime, Price attributes, and the ClosetHistory not created and that is true because the table between tow tables in many to many relation not represented as an entity, But if i remove the startdate and enddate, price fields from ClosetHistory table and re pdate my entities it works and the relation is created
but then i can't save the starttime and endtime and price for the closets

can you help me please ??

and i am sorry for all this explanation, and i am sorry for my English language i know it's very bad :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2024-10-25 22:42:32

您可以使用 4 表方案和一对插入和删除触发器来解决您的问题。在此处输入图像描述

CREATE TRIGGER [dbo].[AddTrigger]
   ON [dbo].[ClosetJoint]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    DECLARE @JointId INT;
    DECLARE @ClosetId INT;

    SET @ClosetId = (SELECT ClosetID FROM inserted);
    SET @JointId = (SELECT JointID FROM inserted);

    INSERT INTO ClosetHistory(ClosetID, JointID, StartTime)
     VALUES(@ClosetId, @JointId, GETDATE())
END

CREATE TRIGGER [dbo].[DeleteTrigger]
   ON [dbo].[ClosetJoint]
   AFTER Delete
AS 
BEGIN
    SET NOCOUNT ON;
    DECLARE @JointId INT;
    DECLARE @ClosetId INT;

    SET @ClosetId = (SELECT ClosetID FROM deleted);
    SET @JointId = (SELECT JointID FROM deleted);

    UPDATE ClosetHistory SET EndTime = GETDATE()
    WHERE ClosetID = @ClosetId AND JointID = @JointId AND EndTime IS NULL
END

You can use 4 table scheme and a pair of Insert and Delete triggers to resolve your issue.enter image description here

CREATE TRIGGER [dbo].[AddTrigger]
   ON [dbo].[ClosetJoint]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    DECLARE @JointId INT;
    DECLARE @ClosetId INT;

    SET @ClosetId = (SELECT ClosetID FROM inserted);
    SET @JointId = (SELECT JointID FROM inserted);

    INSERT INTO ClosetHistory(ClosetID, JointID, StartTime)
     VALUES(@ClosetId, @JointId, GETDATE())
END

CREATE TRIGGER [dbo].[DeleteTrigger]
   ON [dbo].[ClosetJoint]
   AFTER Delete
AS 
BEGIN
    SET NOCOUNT ON;
    DECLARE @JointId INT;
    DECLARE @ClosetId INT;

    SET @ClosetId = (SELECT ClosetID FROM deleted);
    SET @JointId = (SELECT JointID FROM deleted);

    UPDATE ClosetHistory SET EndTime = GETDATE()
    WHERE ClosetID = @ClosetId AND JointID = @JointId AND EndTime IS NULL
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文