Entity Framework Code First is a method of developing software applications that utilizes the Entity Framework and focuses on the code-first approach. The modeling workflow of Entity Framework Code First involves defining domain classes or models that represent the objects in the application. These models are then used to create a database using the DbContext and DbSets classes.
A database connection is established and managed by the DbContext class, and the database is created based on the models defined in the domain classes. Code First Migrations are used to manage database changes and keep track of the evolution of the database over time.
The mappings between the domain classes and the database are configured using either Data Annotations or Fluent API. Data Annotations are attributes that are added to the models to specify the properties of the database, such as the primary key, data type, etc. Fluent API is a more flexible way of defining mappings that allows developers to use a fluent API syntax instead of data annotations.
public class PostAnswer
{
public int PostAnswerId { get; set; }
public string Content { get; set; }
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
public DbSet<Post> Posts { get; set; }
using System.Data.Entity;
using CodeFirst.Models;
public class ForumContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<PostAnswer> PostAnswers { get; set; }
public DbSet<Tag> Tags { get; set; }
}
In the same way as when we use database first or model first approach
var db = new ForumContext();
var category = new Category { Parent = null, Name = "Database course", };
db.Categories.Add(category);
var post = new Post();
post.Title = "Срока на домашните";
post.Content = "Моля удължете срока на домашните";
post.Type = PostType.Normal;
post.Category = category;
post.Tags.Add(new Tag { Text = "домашни" });
post.Tags.Add(new Tag { Text = "срок" });
db.Posts.Add(post);
db.SaveChanges();
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
public class ForumContext : DbContext
{
public ForumContext()
: base("ForumDb")
{ }
}
<connectionStrings>
<add name="ForumDb" connectionString="Data Source=.;Initial Catalog=ForumDb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Database.SetInitializer(
new MigrateDatabaseToLatestVersion
<ForumContext, Configuration>());
public Configuration()
{
this.AutomaticMigrationsEnabled = true;
this.AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(ForumContext context)
{
/* This method will be called after migrating to
the latest version. You can use the
DbSet<T>.AddOrUpdate() helper extension method
to avoid creating duplicate seed data. E.g. */
context.Tags.AddOrUpdate(new Tag { Text = "срок" });
context.Tags.AddOrUpdate(new Tag { Text = "форум" });
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Tag>().HasKey(x => x.TagId);
modelBuilder.Entity<Tag>().Property(x => x.Text).IsUnicode(true);
modelBuilder.Entity<Tag>().Property(x => x.Text).HasMaxLength(255);
// modelBuilder.Entity<Tag>().Property(x => x.Text).IsFixedLength();
base.OnModelCreating(modelBuilder);
}