Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在codefirst方法中创建表_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 如何在codefirst方法中创建表

C# 如何在codefirst方法中创建表,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我正在用C#使用代码优先的方法创建登录页面,在这个过程中,我在尝试执行代码时遇到了很多错误。我是一个新来的 你能检查一下我的代码,帮我看看我漏掉了什么吗 规则未创建和获取多个错误。你的帮助会帮助我理解这件事出了什么问题 我的班级“Class1.cs” 公共类登录 { [必需] 公共字符串用户名{get;set;} [必需] 公共字符串密码{get;set;} } } 公共类LoginContext:DbContext { public LoginContext():base(“LoginDBCo

我正在用C#使用代码优先的方法创建登录页面,在这个过程中,我在尝试执行代码时遇到了很多错误。我是一个新来的

你能检查一下我的代码,帮我看看我漏掉了什么吗

规则未创建和获取多个错误。你的帮助会帮助我理解这件事出了什么问题

我的班级“Class1.cs”

公共类登录
{
[必需]
公共字符串用户名{get;set;}
[必需]
公共字符串密码{get;set;}
}
}
公共类LoginContext:DbContext
{
public LoginContext():base(“LoginDBConnectionString”)
{
SetInitializer(新的DropCreateDatabaseIfModelChanges());
}
公共数据库集用户名{get;set;}
公共数据库集密码{get;set;}
}
Context.cs

using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.Entity;
using Jquery1.Models;

namespace Jquery1.Models
{
    public class Logincontext: DbContext
    {
        public Logincontext () : base ("LoginDBConnectionString")
        {

        }

        public DbSet<Login> Logins{ get; set; }

    }
  }

class program
{
    static void Main(string[] args)
    {

        using (var ctx = new Logincontext())
        {
            ctx.Database.Create();
        }`enter code here`
    }
}
使用System.Data.Entity.ModelConfiguration.Conventions;
使用System.Linq;
使用System.Web;
使用System.Web.UI.WebControl;
使用System.Data.Entity;
使用Jquery1.模型;
名称空间Jquery1.Models
{
公共类Logincontext:DbContext
{
public Logincontext():base(“LoginDBConnectionString”)
{
}
公共数据库集登录{get;set;}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
使用(var ctx=new Logincontext())
{
ctx.Database.Create();
}`在这里输入代码`
}
}

嗨,让我用
fluent api解释一下这个问题

首先创建您的
DbContext

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("name=MyConnection")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, YourApplication.Migrations.Configuration>("MyConnection")); 
    }
    public DbSet<Users> Users { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //here you can MAP Your Models/Entities, but i am going to show you something more interesting. so keep up. 
        modelBuilder.Configurations.Add(new UsersMap());
    }
}
现在继续创建您的
POCO
类。我试着把代码写得非常干净。这就是为什么例如,当我制作一个
模型时,我会为每个
Id
创建一个
EntityBase

public class EntityBase
{
    public int Id { get; set; }
}
并将其应用于我的
车型

public class User: EntityBase
{
    public string Example1{ get; set; }
    public string Example2{ get; set; }
    public string Example3{ get; set; }
}
对于映射,我创建了另一个类,如下所示,并使用
fluentapi

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        //declaring the table name
        ToTable("TblUser");
        //declaring primary key of the table
        HasKey(x => x.Id);
        //declaring a property from poco class is required
        Property(x => x.Example1)
            .IsRequired();
        //etc

    }
}
公共类用户映射:EntityTypeConfiguration
{
公共用户映射()
{
//声明表名
ToTable(“TblUser”);
//声明表的主键
HasKey(x=>x.Id);
//需要从poco类声明属性
属性(x=>x.Example1)
.IsRequired();
//等
}
}

请注意,如果您使用的是fluent api,则不应使用数据注释。快乐编码。

实体框架使用标准或其他概念。如果你想让你的物品相当标准,你不需要提供很多信息。如果希望表具有不同的名称,或者列与标准列不同,则必须使用属性(使用的方法)或fluent API提供额外信息

每个应该成为表的类都应该有一个主键。默认情况下,为类指定属性Id,或为属性指定类的名称,后跟Id:

public class Login
{
    public int Id {get; set;}
    public string UserName {get; set;}
    public string Password {get; set;}
}
public class MyDbContext : DbContext
{
    public DbSet<Login> Logins {get; set;}
}
您将看到使用了这两种方法。两者都有各自的优势。Id的使用会立即显示哪个属性是主键。LoginId也可以用作外键。仅名称LoginId不足以确定它是主键还是外键

默认值通常是项目集合的复数形式,其中项目是单数形式。在这里,您将看到登录为类,登录为此类的一组对象

帮助我使用实体框架走上正轨的那篇文章是


本教程将告诉您如何使用默认值、如何使用外键创建一对多关系、多对多、各种继承策略,以及如果您对默认模型不满意该怎么办。

您将不得不提供更多信息。你的密码在哪里?什么错误?它们被扔到哪里去了?@David Thnx,我的代码附件提供了你在这里得到的错误,否则人们很难帮助你,为什么两个例子中的LoginContext都不一样?用户名和密码类在哪里?DbSet表示数据库中的一个表,我认为您将它们与列混淆了。所以,不要忘记在生产环境中禁用自动迁移。当前配置可能会导致不必要/意外的数据丢失。
public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        //declaring the table name
        ToTable("TblUser");
        //declaring primary key of the table
        HasKey(x => x.Id);
        //declaring a property from poco class is required
        Property(x => x.Example1)
            .IsRequired();
        //etc

    }
}
public class Login
{
    public int Id {get; set;}
    public string UserName {get; set;}
    public string Password {get; set;}
}
public class MyDbContext : DbContext
{
    public DbSet<Login> Logins {get; set;}
}
public int LoginId {get; set;}