Mik*_* S. 18 .net c# sql entity-framework
通常,我倾向于使用以下驼峰大小写约定我的sql数据库列:
camelCase(注意第一个字母是小写的).
但是在使用C#时,我喜欢用以下约定命名对象的公共属性:
CamelCase(注意第一个是在uppwer的情况下).
实体框架的默认行为是将创建的类的属性命名为与它们在数据库中的相对列名相匹配.
是否有项目/解决方案级别的属性可以更改以解决此问题?
And*_*Gis 14
就在这里.在这里你可以看到完整的例子:
using System;
using System.Data.Entity;
namespace ConsoleApplication1
{
class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Properties().Configure(c =>
{
var name = c.ClrPropertyInfo.Name;
var newName = char.ToLower(name[0]) + name.Substring(1);
c.HasColumnName(newName);
});
}
public MyDbCondenxt(string cs) : base(cs)
{
}
public DbSet<MyModel> MyModels { get; set; }
}
class Program
{
static void Main(string[] args)
{
var context = new MyDbContext ("DefaultConnection");
context.MyModels.Add(new MyModel{SomeText = "hello"});
context.SaveChanges();
Console.ReadLine();
}
}
class MyModel
{
public int Id { get; set; }
public string SomeText { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
属性名称为"SomeText",列名为"someText".
我不知道解决方案级别,但您可以在实体上设置属性
[Table("myEntity")]
public class MyEntity{}
Run Code Online (Sandbox Code Playgroud)