1、我使用ABP的启动模板(http://www.aspnetboilerplate.com/Templates)来创建一个Web应用程序。
2、加载项目解决方案
在abp根据模板创建解决方案后,编译报错,提示某个包的版本不对。
解决方案:https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json
3、在.Core中创建entity
生成poco实体类的工具:EntityFramework Reverse POCO Generator,可以根据数据库生成具有DataAnnotations的实体类:
可以通过vs的扩展安装,也可以通过这个地址:https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator#qna
生成DataAnnotations特性,需要在T4中做如下设置更改:
- Settings.ConnectionStringName = "Test"; // Searches for this connection string in config files listed below in the ConfigFilenameSearchOrder setting
- Settings.UseDataAnnotations = true; // If true, will add data annotations to the poco classes.
- Settings.UseDataAnnotationsSchema = true; // UseDataAnnotations must also be true. If true, will add data annotations schema to the poco classes.
- 在dataAnnotation上加长字段长度,针对char和varcahr:
if (Settings.UseDataAnnotations)
{
if(c.Ordinal > 1 && !commentWritten)
WriteLine(string.Empty); // Leave a blank line before the next property foreach (var dataAnnotation in c.DataAnnotations)
{
//将char、varchar的dataAnnotation上加上长度,如varchar(10),以兼容.net core
if(dataAnnotation.ToString().StartsWith("Column"))
{
if(dataAnnotation.ToString().Contains("varchar"))
WriteLine(" [" + dataAnnotation.ToString().Replace("varchar","varchar("+c.MaxLength+")") + "]");
else if(dataAnnotation.ToString().Contains("char"))
WriteLine(" [" + dataAnnotation.ToString().Replace("char","char("+c.MaxLength+")")+ "]");
}
else
{
WriteLine(" [" + dataAnnotation + "]");
} }
}
4、方法EntityFramworkCore的DBContext增加DBSet
======================================
Add-Migration "Initial"
update-database
======================================