1

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();

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

对不起noobness。

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

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

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

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

4

1 回答 1

0

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

于 2010-11-18T05:18:34.893 回答