数据库未使用Codefirst实体添加到sqlserver

时间:2019-07-23 06:43:22

标签: c# sql-server entity-framework

我开始了一个新项目,我想首先使用实体​​框架代码创建数据库,我之前曾尝试过并且可以使用,但是这次没有用。 当我执行add-migration命令时,它找不到我的任何模型,而我只看到以下内容:

public partial class Create_DB : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

有时它可以工作并且可以识别我的模型,但是当我想执行Update-Database命令时,它不会将数据库添加到sql server。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Accounting.DataLayer.Models;

namespace Accounting.DataLayer.Contexts
{
    public class MyContext : DbContext
    {


        public DbSet<AppUsers> MyProperty { get; set; }
        public DbSet<ManageChecks> manageChecks { get; set; }
        public DbSet<Customers> Customers { get; set; }
        public DbSet<DebtorAndCreditManagment> DACManagment { get; set; }
        public DbSet<FinancialAccounts> FinancialAccounts { get; set; }
        public DbSet<FinancialManagment> FinancialManagment { get; set; }
        public DbSet<Messages> Messages { get; set; }
        public DbSet<UsersLoginHistory> UsersLoginHistory { get; set; }

    }
}

//Models

  public class AppUsers
    {
        [Key]
        public int UserId { get; set; }
        [Required]
        [MaxLength(100)]
        public string UserRealName { get; set; }
        [Required]
        [MaxLength(40)]
        public string Username { get; set; }
        [Required]
        [MaxLength(100)]
        public string UserPassword { get; set; }
        [Required]
        [MaxLength(10)]
        public string UserType { get; set; }
        [Required]
        public string UserImageAddress { get; set; }
    }
 public class Customers
    {
        [Key]
        public int CustomerId { get; set; }
        [Required]
        public int UserId { get; set; }
        [Required]
        [MaxLength(100)]
        public string CustomerFullName { get; set; }
        [MaxLength(12)]
        public string CustomerPhoneNumber { get; set; }
        [MaxLength(11)]
        public string CustomerMobileNumber { get; set; }
        [MaxLength(50)]
        public string CustomerEmail { get; set; }
        [MaxLength(300)]
        public string CustomerHouseAddress { get; set; }
        [MaxLength(300)]
        public string CustomerWorkAddress { get; set; }
    }

 public class DebtorAndCreditManagment
    {
        [Key]
        public int DACId { get; set; }
        [Required]
        public int UserId { get; set; }
        [Required]
        public int CustomerId { get; set; }
        [Required]
        [MaxLength(100)]
        public string CustomerFullName { get; set; }
        [Required]
        [MaxLength(10)]
        public string DACStatus { get; set; }
        [Required]
        public DateTime StartDate { get; set; }
        [Required]
        public DateTime EndDate { get; set; }
        [Required]
        public int DACNetAmount { get; set; }
        [Required]
        public int DACProfit { get; set; }
        [Required]
        public int DACFinalAmount { get; set; }
        [Required]
        public int DACPaidAmount { get; set; }
        [Required]
        public int DACRemainedAmount { get; set; }


    }
public class FinancialAccounts
    {
        [Key]
        public int AccountId { get; set; }
        [Required]
        public int UserId { get; set; }
        [Required]
        [MaxLength(100)]
        public string AccountOwnerName { get; set; }
        [Required]
        [MaxLength(50)]
        public string AccountBankName { get; set; }
        [MaxLength(16)]
        public string AcoountCardNumber { get; set; }
        [MaxLength(20)]
        public string AccountNumber { get; set; }
        [MaxLength(24)]
        public string AccountShabaNumber { get; set; }
        [Required]
        public DateTime AccountOpeningDate { get; set; }
        [Required]
        public DateTime AccountExpDate { get; set; }
        [Required]
        public int AccountCash { get; set; }

    }
public class FinancialManagment
    {
        [Key]
        public int TransId { get; set; }
        [Required]
        public int UserId { get; set; }
        [Required]
        [MaxLength(10)]
        public string TransType { get; set; }
        [Required]
        public int CustomerId { get; set; }
        [Required]
        [MaxLength(100)]
        public string CustomerFullName { get; set; }
        [Required]
        public int TransAmount { get; set; }
        [Required]
        public DateTime TransDate { get; set; }
        [MaxLength(300)]
        public string TransDescription { get; set; }


    }
 public class ManageChecks
    {
        [Key]
        public int CheckId { get; set; }
        [Required]
        public int UserId { get; set; }
        [Required]
        [MaxLength(8)]
        public string CheckType { get; set; }
        [Required]
        [MaxLength(100)]
        public string CheckOwner { get; set; }
        [Required]
        [MaxLength(20)]
        public string CheckAccountNumber { get; set; }
        [Required]
        [MaxLength(24)]
        public string CheckShabaNumber { get; set; }
        [Required]
        public int CheckAmount { get; set; }
        [Required]
        public DateTime CheckDate { get; set; }
        [Required]
        [MaxLength(100)]
        public string CheckPayTo { get; set; }
        [MaxLength(300)]
        public string Description { get; set; }
        [Required]
        [MaxLength(15)]
        public string CheckStatus { get; set; }

    }
 public class Messages
    {
        [Key]
        public int MessageId { get; set; }
        [Required]
        [MaxLength(400)]
        public string MessageText { get; set; }
        [Required]
        public int MessageLevel { get; set; }


    }
 public class UsersLoginHistory
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [MaxLength(40)]
        public string Username { get; set; }
        [Required]
        public DateTime LoginDate { get; set; }
    }

连接字符串:

 <connectionStrings>
    <add name="MyContext"
         connectionString="Data Source=.;Initial Catalog=fgdg;Integrated Security=true"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

0 个答案:

没有答案