MVC 4 - 连接到自己的数据库 [英] MVC 4 - Connecting to own database

查看:96
本文介绍了MVC 4 - 连接到自己的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序,我已经创建了一个名为UserModel.cs的模型类,在这个类中我编写代码为:



namespace Demo2.Models

{

[表(User_Mst)]

公共类UserModel

{

[Key]

public Int64 Id {get;组; } $ / $
[StringLength(50)]

public String User_Name {get;组; }

[StringLength(50)]

public String Password {get;组; } b / b
公共类UserContextDB:DbContext

{

[Key]

public DbSet< usermodel>用户{get;组; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.Conventions.Remove< pluralizingtablenameconvention>();

}

}



}



我做了迁移使用命令enable-migrations -contexttypename UserContextDB



然后我创建了控制器(使用名为具有读/写操作和视图的MVC控制器,使用实体框架的模板)自动添加的.views对应于这个模型,我还在名为UserContextDB的连接字符串中定义了我自己的数据库。



但每当我试图从创建保存值时查看然后这些值不保存在我自己的数据库中,它使用其他数据库。



我想将所有值存储在我自己的数据库中。请给我帮助。



谢谢

解决方案

为自己的类使用另一个DBContext,UserContextDB是对于roleMemberShip并且可能会导致问题,当我想添加类时我添加自己的DBContext并将roleMemberShip DBContext(UserContextDB)更改为我的DBContext



Code First with Entity Framework 5 using MVC4 and MVC Scaffold [ ^

I have developed an application in this i have created a model class named UserModel.cs, in this class i write code as:

namespace Demo2.Models
{
[Table("User_Mst")]
public class UserModel
{
[Key]
public Int64 Id { get; set; }
[StringLength(50)]
public String User_Name { get; set; }
[StringLength(50)]
public String Password { get; set; }
}
public class UserContextDB : DbContext
{
[Key]
public DbSet<usermodel> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<pluralizingtablenameconvention>();
}
}

}

I have done migration using command "enable-migrations -contexttypename UserContextDB"

then i created controller (using template named "MVC controller with read/write actions and views, using Entity Framework").views automatically added correspond to this model, i also defined my own database in connection string named "UserContextDB".

But whenever i tried to save values from "create" view then these values are not saving in my own database, it uses other database.

I want to store all values in my own database. Please provide me help.

Thanks

解决方案

Use another DBContext for your own classes , the UserContextDB is for roleMemberShip and may cause problem , when i want to add class i add my own DBContext and change roleMemberShip DBContext(UserContextDB) to my DBContext

Code First with Entity Framework 5 using MVC4 and MVC Scaffold[^]


这篇关于MVC 4 - 连接到自己的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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