我正在使用Entity Framework 5中的数据库第一个模型,当我尝试添加一行时,出现以下错误: “在程序集中没有找到任何视图,或者可以为表'ui_renewals'生成视图。”
该表存在于我的EDMX中,模板生成了一个ui_renewals类。我已经从EDMX中删除了表并使用“从数据库更新模型”选项再次添加它,我得到了同样的错误。为它创建一个单独的连接解决了这个问题,但这似乎是一个不太理想的解决方案(更像是一个kludge),更不用说它会让它在未来更难维护。
有关如何解决此问题的任何想法,以便我可以在ui_renewals中添加或更新(我已尝试过两行)?
以下是我目前正在使用的代码 - 之前的区别仅在于使用db作为DBContext而不是ui(是的,收据拼写错误 - 需要爱遗留的东西)
[HttpPost]
public bool UpdateTeacher(string login_id, string password, UIRenewal data)
{
if (ModelState.IsValid)
{
// map from UIRenewal VM to ui_renewal
ui_renewals Renewal = Mapper.Map<UIRenewal, ui_renewals>(data);
// check to see if this is a new entry or not
var tmp = ui.ui_renewals.Find(Renewal.reciept);
if (tmp == null)
ui.ui_renewals.Add(Renewal);
else
{
// mark as modified
db.Entry(Renewal).State = EntityState.Modified;
}
// save it
try
{
ui.SaveChanges();
}
catch (DBConcurrencyException)
{
return false;
}
return true;
}
return false;
}
我应该提一下,我在模型中有一个视图(v_recent_license)。
答案 0 :(得分:3)
我知道这是一个非常古老的问题,但由于我没有找到这样的其他主题,我会发布我的答案。
我抛出了相同的异常。我发现,在尝试优化EF性能失败后,根据here发现的建议,我在EF .edmx代码隐藏中留下了这段代码:
<EntityContainerMapping StorageEntityContainer="XXXModelStoreContainer" CdmEntityContainer="YYYEntities" GenerateUpdateViews="false">
我删除了GenerateUpdateViews="false"
字符串,所有字符串再次正常运行。
(在我看来,异常消息有点误导)。