Entity Framework Basics
This commit is contained in:
@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace EntityFrameworkBasics
|
||||
{
|
||||
/// <summary>
|
||||
/// The database representational model for our application
|
||||
/// </summary>
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// The settings for the application
|
||||
/// </summary>
|
||||
public DbSet<SettingsDataModel> Settings { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor, expecting database options passed in
|
||||
/// </summary>
|
||||
/// <param name="options">The database context options</param>
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Fluent API
|
||||
|
||||
modelBuilder.Entity<SettingsDataModel>().HasIndex(a => a.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs
Normal file
31
ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace EntityFrameworkBasics
|
||||
{
|
||||
/// <summary>
|
||||
/// Our Settings database table representational model
|
||||
/// </summary>
|
||||
public class SettingsDataModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The unique Id for this entry
|
||||
/// </summary>
|
||||
[Key]
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The settings name
|
||||
/// </summary>
|
||||
/// <remarks>This column is indexed</remarks>
|
||||
[Required]
|
||||
[MaxLength(256)]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The settings value
|
||||
/// </summary>
|
||||
[Required]
|
||||
[MaxLength(2048)]
|
||||
public string Value { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user