实体框架 - 第一资本化属性名字母 [英] Entity Framework - Capitalizing first property name letter

查看:157
本文介绍了实体框架 - 第一资本化属性名字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一般情况下,我更倾向于使用下面的驼峰约定来命名我的SQL数据库列:

In general, I tend to name my sql database columns using the following camel case convention:

驼峰(注意第一个字母是小写)

camelCase (notice that the first letter is in lower case).

但随着C#工作时,我喜欢来命名我的对象在以下约定公共属性:

But when working with C#, I like to name my object's public properties in the following convention:

驼峰(注意首先是uppwer情况)。

CamelCase (notice the first is in uppwer case).

实体框架的默认行为是命名创建的类的属性,因为它们是在数据库中,以匹配其相对列名。

Entity Framework's default behaviour is to name the created classes' properties to match their relative column names as they are in the database.

有没有可以在为了解决这个问题呢?

Is there any property in the project/solution level which can be changed in order to solve this issue?

推荐答案

是的,有。在这里,您可以看到完整的示例:

Yes there is. Here you can see the full example:

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; }
    }


}



属性名称为SomeText则会和列名是SomeText则会。

The property name is "SomeText" and the column name is "someText".

这篇关于实体框架 - 第一资本化属性名字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆