您的位置:首页 > 编程语言 > C#

在C#中使用EF(EntityFramework)插入数据到DB的异常

2017-04-07 12:55 435 查看
我在DB中建了一张表,试图用EF的方式保存数据。

代码很简单:

            using (masterEntities curDBContext = new masterEntities())

            {

                curCost.ID = Guid.NewGuid().ToString();

                curDBContext.Home_Cost.Add(curCost);

                curDBContext.SaveChanges();

            }

但是在保存时出现异常:

Unable to update the EntitySet 'Home_Cost' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.

查找原因,发现表Home_Cost没有设置主键。设置后,问题解决。

经验分享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# EF EntityFramework