龙空技术网

EF(DBFirst)搭建WebApi项目

媒体专属号 167

前言:

此时各位老铁们对“log4netdll官网下载”可能比较关切,同学们都需要剖析一些“log4netdll官网下载”的相关资讯。那么小编在网上网罗了一些关于“log4netdll官网下载””的相关文章,希望各位老铁们能喜欢,朋友们一起来了解一下吧!

一、设计业务数据库

ps:中文乱码问题

[execute ]sp_addextendedproperty N'MS_Description',    N%.q:COMMENT%,    N'user', N'dbo', N'table', N%.q:TABLE%, N'column', N%.q:COLUMN%

[execute ]sp_addextendedproperty N'MS_Description',    N%.q:COMMENT%,    N'user', N'dbo', N'table', N%.q:TABLE%

二、建表

三、建解决方案(vs2017)

1、在Models新建对应的实体

/// <summary>    /// 学生表    /// </summary>    public class Student    {        /// <summary>        /// ID        /// </summary>        public string ID { get; set; }        /// <summary>        /// 学号        /// </summary>        public string STUNO { get; set; }        /// <summary>        /// 创建日期        /// </summary>        public DateTimeOffset? CREATEDATE { get; set; }        /// <summary>        /// 修改日期        /// </summary>        public DateTimeOffset? UPDATEDATE { get; set; }    }

2、新建文件夹DTOModels,用于收集数据

3、新建DAL文件夹(用于数据访问)

4、添加引用

添加引用EntityFramework.dll

添加引用EntityFramework.SqlServer.dll

设置属性“复制到本地”为true

5、新建BaseContext文件,继承

 public class BaseContext : DbContext    {        static BaseContext()        {            Database.SetInitializer<BaseContext>(null);        }        /// <summary>        /// 学生表        /// </summary>        public DbSet<Student> Students { get; set; }                protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            //    //解决DbContext保存decimal类型数据到数据库,默认只会保存小数点后的前2位小数,其余均置0的问题,这样就会保存小数点7位了            //    modelBuilder.Entity<Student>().Property(p => p.Qty).HasPrecision(18, 6);            //    modelBuilder.Entity<Student>().Property(p => p.UseCount).HasPrecision(18, 6);        }    }

6、新建Business文件夹(用于存放业务逻辑)

7、新建API控制器

The underlying provider failed on Open(无配置项目)

webconfig

8、新增数据库配置

<connectionStrings>

<add name="BaseContext" connectionString="Server=服务器地址;Database=数据库名称;User id=数据库用户名;Password=数据库密码" providerName="System.Data.SqlClient" />

</connectionStrings>

InnerException = {"Invalid object name 'dbo.Students'."}---表名错误

场景一、新增数据库字段(允许比数据库少字段)

数据库新增字段后,手动更新实体后,要重新编译项目这样保存数据才会到数据库

场景二、调整数据库类型,非空类型要加?

The 'AGE' property on 'Student' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.Decimal'.

9、新建文件夹Common,用于存放公共方法、工具类等

引入log4net

config文件+dll

log4net不能记录日志,IsErrorEnabled值为false

Properties下面的AssemblyInfo.cs中加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = "(具体路径)log4net.config", Watch = true)]

标签: #log4netdll官网下载 #ef添加数据