EF 配置

DbContext
    public class ZSZDbContext : DbContext
    {
        //ILog ILogger 容易混淆
        private static ILog log = LogManager.GetLogger(typeof(ZSZDbContext));

        public ZSZDbContext() : base("name=connstr")
        //name=conn1表示使用连接字符串中名字为conn1的去连接数据库
        {
            Database.SetInitializer<ZSZDbContext>(null);
            this.Database.Log = sql =>
            {
                log.DebugFormat("EF执行SQL:{0}", sql);
            };
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
        }
        public DbSet<AdminUserEntity> AdminUsers { get; set; }
        public DbSet<UserEntity> Users { get; set; }
        public DbSet<CityEntity> Cities { get; set; }
        public DbSet<CommunityEntity> Communities { get; set; }
        public DbSet<PermissionEntity> Permissions { get; set; }
        public DbSet<RegionEntity> Regions { get; set; }
        public DbSet<RoleEntity> Roles { get; set; }
        public DbSet<SettingEntity> Settings { get; set; }
        public DbSet<AttachmentEntity> Attachments { get; set; }
        public DbSet<HouseEntity> Houses { get; set; }
        public DbSet<HouseAppointmentEntity> HouseAppointments { get; set; }
        public DbSet<IdNameEntity> IdNames { get; set; }
        public DbSet<HousePicEntity> HousePics { get; set; }
        public DbSet<AdminLogEntity> AdminUserLogs { get; set; }
    }
Entity
    public class HouseEntity : BaseEntity
    {
        public long CommunityId { get; set; }
        public virtual CommunityEntity Community { get; set; }
        public long RoomTypeId { get; set; }
        public virtual IdNameEntity RoomType { get; set; }
        public string Address { get; set; }
        public int MonthRent { get; set; }
        public long StatusId { get; set; }
        public virtual IdNameEntity Status { get; set; }
        public decimal Area { get; set; }
        public long DecorateStatusId { get; set; }
        public virtual IdNameEntity DecorateStatus { get; set; }
        public int TotalFloorCount { get; set; }
        public int FloorIndex { get; set; }
        public long TypeId { get; set; }
        public virtual IdNameEntity Type { get; set; }
        public string Direction { get; set; }
        public DateTime LookableDateTime { get; set; }
        public DateTime CheckInDateTime { get; set; }
        public string OwnerName { get; set; }
        public string OwnerPhoneNum { get; set; }
        public string Description { get; set; }

        public virtual ICollection<AttachmentEntity> Attachments { get; set; } = new List<AttachmentEntity>();
        public virtual ICollection<HousePicEntity> HousePics { get; set; } = new List<HousePicEntity>();
    }
Config
    class HouseConfig : EntityTypeConfiguration<HouseEntity>
    {
        public HouseConfig()
        {
            ToTable("T_Houses");
            HasRequired(h => h.Community).WithMany().HasForeignKey(h => h.CommunityId).WillCascadeOnDelete(false);
            HasRequired(h => h.RoomType).WithMany().HasForeignKey(h => h.RoomTypeId).WillCascadeOnDelete(false);
            HasRequired(h => h.Status).WithMany().HasForeignKey(h => h.StatusId).WillCascadeOnDelete(false);
            HasRequired(h => h.DecorateStatus).WithMany().HasForeignKey(h => h.DecorateStatusId).WillCascadeOnDelete(false);
            HasRequired(h => h.Type).WithMany().HasForeignKey(h => h.TypeId).WillCascadeOnDelete(false);
            Property(h => h.Address).IsRequired().HasMaxLength(128);
            Property(h => h.Description).IsOptional();
            Property(h => h.Direction).IsRequired().HasMaxLength(20);
            Property(h => h.OwnerName).IsRequired().HasMaxLength(20);
            Property(h => h.OwnerPhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);
        }
    }

 

转载于:https://www.cnblogs.com/kikyoqiang/p/10847172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用 Entity Framework (EF) 配置 MySQL 数据库的问题,你可以按照以下步骤进行配置: 1. 首先,确保你已经安装了 MySQL 数据库和 MySQL Connector/NET。你可以从 MySQL 官方网站下载并安装它们。 2. 在你的 .NET 项目中,通过 NuGet 包管理器安装 Entity Framework。你可以在 Visual Studio 中打开 NuGet 包管理器控制台,并运行以下命令: ``` Install-Package EntityFramework ``` 3. 在你的项目中创建一个派生自 `DbContext` 类的类,用于表示数据库上下文。例如,你可以创建一个名为 `MyDbContext` 的类。 4. 在 `MyDbContext` 类中,重写 `OnConfiguring` 方法,并在其中设置连接字符串。以下是一个示例: ```csharp public class MyDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connectionString = "server=localhost;database=your_database;uid=your_username;password=your_password"; optionsBuilder.UseMySQL(connectionString); } } ``` 注意替换 `your_database`、`your_username` 和 `your_password` 为你的实际数据库名称、用户名和密码。 5. 在你的项目中创建实体类,用于表示数据库中的表。每个实体类都应该映射到数据库中的一个表。 6. 在 `MyDbContext` 类中,使用 `DbSet<TEntity>` 属性定义一个属性来表示每个实体类对应的表。例如: ```csharp public DbSet<User> Users { get; set; } ``` 这里的 `User` 是一个实体类,它将映射到数据库中的一个表。 7. 在你的项目中使用 Entity Framework 提供的数据访问方法,例如 `Add`、`Remove` 和 `SaveChanges` 来操作数据库。 这是一个简单的示例,用于配置和使用 Entity Framework 连接到 MySQL 数据库。你可以根据你的具体需求进行更多的配置和操作。希望对你有帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值