如何使用模型binder-mvc.net的多个模型 [英] How to use multiple model with model binder-mvc.net
问题描述
嗨我需要帮助在我的应用程序DAL层中使用DBContext使用多个模型。
以下是需要修改的代码
提前感谢您。
我尝试了什么:
使用System;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Configuration;
使用System.Linq;
使用System.Reflection;
使用System.Web;
命名空间XYZ.DAL
{
公共类ApplicationDAL:DbContext {
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity< a1>()。ToTable(A1);
// modelBuilder .Entity< A2>()ToTabl。 e(A2);
// modelBuilder.Entity< a3>()。ToTable(A3);
}
public DbSet< a1> A1s {得到;组; }
public DbSet< a2> A2s {get;组; }
public DbSet< a3> A3s {得到;组; }
}
}
hi I need help to use multiple models in my application DAL layer using DBContext.
Following is the code which needs to be modified
Thanking you in Advance.
What I have tried:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Reflection;
using System.Web;
namespace XYZ.DAL
{
public class ApplicationDAL :DbContext {
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<a1>().ToTable("A1");
//modelBuilder.Entity<a2>().ToTable("A2");
// modelBuilder.Entity<a3>().ToTable("A3");
}
public DbSet<a1> A1s { get; set; }
public DbSet<a2> A2s { get; set; }
public DbSet<a3> A3s { get; set; }
}
}
推荐答案
实体框架自定义代码优先约定(EF6以上) [ ^ ]
MVC的架构如此只是请求的一个模型。如果你的模型中需要多个对象,那么创建一个自定义类来引用它们
MVC is architected such there is only one model for a request. If you need multiple objects in your model then create a custom class to reference them
public class MyViewModel
{
public ObjectA ObjectA { get; set; }
public ObjectB ObjectB { get; set; }
}
然后您将使用MyViewModel作为模型。
You would then use MyViewModel as the model.
这篇关于如何使用模型binder-mvc.net的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!