使用实体框架实现存储库模式 [英] Implementing Repository Pattern With Entity Framework

查看:65
本文介绍了使用实体框架实现存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据您的示例在Entity Framework数据模型上创建存储库/规范模式.但是,尝试添加记录时出现错误.错误状态找不到EntitySet名称"XXX".它具有正确的名称,但找不到它.到达添加"时会生成错误:

I am attempting to create a Repository/Specification pattern on my Entity Framework data model based on your example. However, I get an error when attempting to add the records. The error states The EntitySet name ''XXX'' could not be found. It has the correct name, but cannot find it. It generates the error when the Add is reached:

ProductManger testProduct = new ProductManger();
List<Product> newProductList = new List<Product>
Console.WriteLine("Adding New Product.......");

For (int i = 1; i <= 5; i++)
{
string productName = "Product Name";
Product prod = AddDefaultProduct(productName)
newProductList.Add(prod);
}



哪个叫...



Which calls...

public void Add(E entity)
{
    _ctx.AddObject(entity.GetType().Name, entity);
}



如果我注释掉程序运行,则可以将此列表写到屏幕上,但不能将其添加到数据库中.

请提供有关如何纠正正在生成的错误的建议.



If I comment this out the program runs, I can write this list out to the screen, but cannot add it to the database.

Please advise as to how to correct the error that is being generated.

推荐答案

您还需要在"="的另一侧指定列表类型. .似乎没有必要,因为您已经给定了类型,但是当将其作为参数传递时,这是行不通的.因此更改:
You need to specify the list type also on the other side of the ''=''. It seems unnecessary because you already gave the type but when passing it as parameter this doesn''t work. So change:
List<product> newProductList = new List


至:


to:

List<Product> newProductList = new List<Product>



祝你好运!



Good luck!


这篇关于使用实体框架实现存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆