模型类和映射 [英] Model class and Mapping
问题描述
我定义了Client类。我也有Mapping\ClientMap.cs文件,该文件使用Fluent API定义列名。但是,我不确定如何调用它,因为我没有看到ClientMap.cs代码正在执行。
I have the Client class defined. I also have Mapping\ClientMap.cs file that defines column names using Fluent API. However, I am not sure how to "invoke" it as I don't see the ClientMap.cs code being executed.
我也有这个:
namespace CardNumbers.Data
{
public class Repository : DbContext, IRepository
{
public DbSet<Client> Clients { get; set; }
public DbSet<ClientOrder> ClientOrders { get; set; }
public DbSet<Reorder> Reorders { get; set; }
public DbSet<Operator> Operators { get; set; }
我知道这不是一个很好的做法,但这就是我们使用
which I know is not a very good practice, but that's how we were building the application with the instructor.
所以,我的问题是-我需要添加什么以确保在运行时调用Fluent API代码?
So, my question is - what do I need to add to make sure the Fluent API code is invoked in run-time?
谢谢。
推荐答案
流利的映射应该添加到您的 存储库
这样的类
The fluent mapping should be added at the bottom of your "Repository
" class like this
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//add fluent api code
}
原因我引用您的存储库是,您实际上并没有在其中实现存储库模式。您确实应该将此定义为特定的DbContext,然后拥有一个可以容纳多个上下文的存储库。请参阅有关实现存储库的写得很好的文章: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using- mvc /在一个asp-net-mvc应用程序中实现存储库和单元工作模式
The reason I am quoting your Repository is that you are not really implementing a repository pattern there. You should really have this defined as a specific DbContext, and then have a repository which can take multiple contexts. See this very well written post about implementing a repository: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
这篇关于模型类和映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!