Linq->实体和外键表问题

发布于 2024-10-03 13:38:36 字数 2257 浏览 4 评论 0原文

C# 和一般编程新手在这里。

我有两张桌子。属性和备忘录。一个属性可以有多个备忘录。我已经,至少我认为我已经正确完成了对象创建的格式。我遇到的问题是 Memo 对象不与 Property 对象一起保存。 Property 对象似乎保存得很好。


由于我是一个堆栈新手,所以我无法将图像直接发布到帖子中,因此我上传了几个显示我的实体图和引用约束对话框的图像。

www.jmtland。 com/Pics/Diagram.png

www.jmtland.com/Pics/Referential%20Constraint.png


            MTDBEntities1 dc = new MTDBEntities1();

            Property newProp = new Property();
            newProp.Address = t_Address.Text.Trim();
            newProp.City = t_City.Text.Trim();
            newProp.State = t_State.Text.Trim();
            newProp.Zip = t_Zip.Text.ToString();
            newProp.PropertyType = cb_PropertyType.Text.Trim();
            if (t_SizeMin.Text.Trim().Length != 0) { newProp.SizeMin = Convert.ToInt64(t_SizeMin.Text); }          // SizeMin is not required, so it won't be passed to the DB if there is no value.
            newProp.SizeMax = Convert.ToInt64(t_SizeMax.Text);
            newProp.SizeMetric = cb_SizeType.Text.Trim();
            if (t_PriceMin.Text.Trim().Length != 0) { newProp.PriceMin = Convert.ToDecimal(t_PriceMin.Text); }     // PriceMin is not required, so it won't be passed to the DB if there is no value.
            newProp.PriceMax = Convert.ToDecimal(t_PriceMax.Text);
            newProp.LeaseType = cb_LeaseType.Text.Trim();
            newProp.WebLink = t_WebLink.Text.Trim();
            newProp.Deleted = false;
            newProp.DateDeleted = null;
            newProp.DateCreated = DateTime.Now;

            Memo newMemo = new Memo();
            newMemo.Memo1 = t_PropertyMemo.Text.Trim();
            newMemo.MemoDateCreated = DateTime.Now;

            newProp.Memos.Add(newMemo);
            dc.AddToProperties(newProp);
            dc.SaveChanges();

过去两天我一直在多个网站上寻找解决此问题的方法论坛。我遵循了太多的例子,以至于我几乎忘记了我的原始代码。

抱歉,我很笨。

更新: 我尝试先保存属性表,然后保存备注表。 - 也不起作用。

我已经运行了调试器,似乎有与 Memo 对象关联的数据,并且 Property 对象包含有问题的 Memo 对象,但由于某种原因,它不会同时保存。

我想了一种不同的解决方法,我可以保存属性,然后执行一个新查询来获取该新对象的 PropertID,然后强制保存返回的 PropertyID 的 Memo 对象。

我对该方法的问题是,这意味着我对实体框架的理解不正确。我可能能够破解它,但如果我不能从一开始就让它正常工作,我担心我以后对相同技术的实施将因我无法从一开始就正确使用而受到阻碍去。我以前涉足过编程,我学到的一件事是,如果你第一次没有学好基础知识,你的其余经验可能会被破坏。

C# and general programming noobie here.

I have two tables. Property and Memo. There can be many Memo's to a single Property. I have, at least I think I do, the format of the object creation done correctly. The issue I do have is the Memo object doesn't save with the Property object. The Property object seems to save just fine.


Since I am a stack-noobie, I cannot post images strait into the post, so I've uploaded a couple which show both my Entities Diagram and the Referential Constraint dialogue.

www.jmtland.com/Pics/Diagram.png

www.jmtland.com/Pics/Referential%20Constraint.png


            MTDBEntities1 dc = new MTDBEntities1();

            Property newProp = new Property();
            newProp.Address = t_Address.Text.Trim();
            newProp.City = t_City.Text.Trim();
            newProp.State = t_State.Text.Trim();
            newProp.Zip = t_Zip.Text.ToString();
            newProp.PropertyType = cb_PropertyType.Text.Trim();
            if (t_SizeMin.Text.Trim().Length != 0) { newProp.SizeMin = Convert.ToInt64(t_SizeMin.Text); }          // SizeMin is not required, so it won't be passed to the DB if there is no value.
            newProp.SizeMax = Convert.ToInt64(t_SizeMax.Text);
            newProp.SizeMetric = cb_SizeType.Text.Trim();
            if (t_PriceMin.Text.Trim().Length != 0) { newProp.PriceMin = Convert.ToDecimal(t_PriceMin.Text); }     // PriceMin is not required, so it won't be passed to the DB if there is no value.
            newProp.PriceMax = Convert.ToDecimal(t_PriceMax.Text);
            newProp.LeaseType = cb_LeaseType.Text.Trim();
            newProp.WebLink = t_WebLink.Text.Trim();
            newProp.Deleted = false;
            newProp.DateDeleted = null;
            newProp.DateCreated = DateTime.Now;

            Memo newMemo = new Memo();
            newMemo.Memo1 = t_PropertyMemo.Text.Trim();
            newMemo.MemoDateCreated = DateTime.Now;

            newProp.Memos.Add(newMemo);
            dc.AddToProperties(newProp);
            dc.SaveChanges();

I've been searching around for a fix for this problem for the last two days on multiple forums. I've followed so many examples that I almost forgot my original code.

Sorry for the noobness.

Update:
I have tried saving the Property Table first, then the Memo table.
-Doesn't work either.

I have run through the debugger and there seems to be data associated with the Memo object as well as the Property object contains the Memo object in question but it, for some reason, doesn't save at the same time.

I've though about a different way around it, where I could save the property, then do a new query to get the PropertID of that new object, then force save the Memo object with the PropertyID returned.

The issue I have with that method is that would mean my understanding of the Entity framework wouldn't be correct. I'd probably be able to hack my way around it but if I can't get it to work properly from the beginning, I fear that my later implementation of the same tech will be hindered by my inability to get it right from the get go. I've dabbled in programming before and the one thing I've learned is if you don't learn the basics right the first time, the rest of your experience can be corrupted.

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

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

发布评论

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

评论(1

帥小哥 2024-10-10 13:38:36

RPM1984 的评论似乎有效。我不知道为什么这比我尝试过的方法有效,但无论如何,我现在可以继续进行下一个 4 小时的单一方法编程!

您有导航属性吗
在“财产”上称为“备忘录”
您的 EDMX 上的实体?我认为最后
线应该是
dc.Properties.AddObject(newProp) –
RPM1984 5小时前

The comment from RPM1984 seems to have worked. I don't know why that worked over what I tried but whatever, I can now move onto my next 4 hour block of programming a single method!

Do you have a navigational property
called "Memos" on the "Property"
entity on your EDMX? I think the last
line should be
dc.Properties.AddObject(newProp) –
RPM1984 5 hours ago

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文