给一个Entity的字段付初始化值(C#)

给一个类去分别赋值,是一个很繁琐切无趣的工作。
那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式。
 
具体的想法就是,利用类的属性,取出所有的字段,然后,根据字段的类型来初始化不同的字段。
/// <summary>
/// エンティティのnull項目が初期化する
///     数字の項目:ゼロ
///     文字列の項目:空白
///     ★値があるの項目はそのままにする
/// </summary>
/// <typeparam name="T">タイプ</typeparam>
/// <param name="entity">エンティティ</param>
/// <returns>初期化結果</returns>
public static T InitEntityValue<T>(T entity)
{
    if (entity == null)
    {
        // ヌルの場合、対象インスタンスを作成する
        entity = (T)Activator.CreateInstance(typeof(T));
    }

    var type = entity.GetType();

    var items = type.GetProperties();
    foreach (var item in items)
    {
        if (item.GetValue(entity, null) != null)
        {
            continue;
        }

        if (typeof(string).Equals(item.PropertyType))
        {
            item.SetValue(entity, string.Empty, null);
        }
        else if (typeof(bool).Equals(item.PropertyType))
        {
            item.SetValue(entity, false, null);
        }
        else if (typeof(int).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(long).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(float).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(double).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(decimal).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else if (typeof(int?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(long?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(float?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(double?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(decimal?).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else if (typeof(Nullable<int>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(Nullable<long>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(Nullable<float>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(Nullable<double>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(Nullable<decimal>).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else
        {
            item.SetValue(entity, InitEntityValue(item.GetValue(entity, null)), null);
        }
    }

    return entity;
}

 

转载于:https://www.cnblogs.com/gekal/p/3746537.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Entity Framework 绑定 SqlServer Express 的 C# 示例 下面是一个使用 Entity Framework 绑定 SqlServer Express 的 C# 示例: 1. 首先,确保你已经安装了 SqlServer Express 和 Microsoft.NET Framework。如果没有,请先下载并安装。 2. 打开 Visual Studio,创建一个新的 C# 项目。 3. 在项目中添加 Entity Framework。在解决方案资源管理器中右键单击“引用”,选择“管理NuGet程序包”选项。 4. 在“NuGet程序包管理器”中搜索“EntityFramework”,然后安装程序包。 5. 在代码文件中,添加以下命名空间: ``` using System.Data.Entity; ``` 6. 创建一个派生自 DbContext 的类。这个类将代表你的数据库上下文。 ``` public class MyDbContext : DbContext { public DbSet<MyEntity> MyEntities { get; set; } } ``` 在上面的代码中,MyDbContext 类继承自 DbContext,并且具有一个名为“MyEntities”的 DbSet 属性。这个属性代表数据库中的一个实体集合。如果你需要处理多个实体,你可以在 MyDbContext 类中添加多个 DbSet 属性。 7. 创建一个派生自 DbContextInitializer 的类。这个类将在 MyDbContext 类实例被创建或数据库首次使用时被调用。 ``` public class MyDbContextInitializer : CreateDatabaseIfNotExists<MyDbContext> { protected override void Seed(MyDbContext context) { base.Seed(context); } } ``` 在上面的代码中,MyDbContextInitializer 类继承自 CreateDatabaseIfNotExists,并重写了 Seed 方法。这个方法将在数据库首次使用时被调用,可以在这个方法中添加一些初始化数据。 8. 在你的代码中,创建 MyDbContext 的实例并使用它来访问数据库。 ``` using (var db = new MyDbContext()) { // 具体操作数据库的代码 } ``` 在上面的代码中,你创建了 MyDbContext 的一个实例,并在 using 块中使用。在 using 块结束时,MyDbContext 实例将被销毁。 9. 通过连接字符串指定连接 SqlServer Express 数据库。可以在 App.config 或 Web.config 文件中添加以下代码: ``` <connectionStrings> <add name="MyDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> ``` 在上面的代码中,Data Source 指定了 SqlServer Express 实例的名称(在这里是“.\SQLEXPRESS”),Initial Catalog 指定了要连接的数据库名称(在这里是“MyDatabase”),Integrated Security=True 指定了使用 Windows 身份验证连接 SqlServer Express。 10. 在 MyDbContext 类中使用上面指定的连接字符串: ``` public class MyDbContext : DbContext { public MyDbContext() : base("name=MyDbContext") { } public DbSet<MyEntity> MyEntities { get; set; } } ``` 在上面的代码中,MyDbContext 类的构造函数使用了连接字符串名称“MyDbContext”。这个名称与前面在 App.config 或 Web.config 文件中定义的名称相同。 现在你已经知道了如何使用 Entity Framework 绑定 SqlServer Express 的 C# 示例。你可以在你的代码中使用这些示例来访问 SqlServer Express 数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值