مقدار دهی اولیهی بانک اطلاعاتی توسط Entity framework Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۱/۱۷ ۱۹:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Magazine
{
public int MagazineId { get; set; }
public string Name { get; set; }
public string Publisher { get; set; }
public List<Article> Articles { get; set; }
}
public class Article
{
public int ArticleId { get; set; }
public string Title { get; set; }
public DateTime PublishDate { get; set; }
public int MagazineId { get; set; }
public Author Author { get; set; }
public int? AuthorId { get; set; }
}
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public List<Article> Articles { get; set; }
} protected override void OnModelCreating (ModelBuilder modelBuilder)
{
modelBuilder.Entity<Magazine>().HasData(new Magazine { MagazineId = 1, Name = "DNT Magazine" });
} migrationBuilder.InsertData(
table: "Magazines",
columns: new[] { "MagazineId", "Name", "Publisher" },
values: new object[] { 1, "DNT Magazine", null }); set IDENTITY_INSERT ON
INSERT INTO "Magazines" ("MagazineId", "Name", "Publisher") VALUES (1, 'DNT Magazine', NULL); modelBuilder.Entity<Magazine>()
.HasData(new Magazine{ MagazineId=2, Name="This Mag" },
new Magazine{ MagazineId=3, Name="That Mag" }
); modelBuilder.Entity<Magazine>().Property(m=>m.Publisher).IsRequired();
"The seed entity for entity type 'Magazine' cannot be added because there was no value provided for the required property 'Publisher'."
public Magazine(string name, string publisher)
{
Name=name;
Publisher=publisher;
} modelBuilder.Entity<Magazine>().HasData(new Magazine("DNT Magazine", "1105 Media")); modelBuilder.Entity<Magazine>().HasData(new {MagazineId=1, Name="DNT Mag", Publisher="1105 Media"}); migrationBuilder.InsertData(
table: "Magazines",
columns: new[] { "MagazineId", "Name", "Publisher" },
values: new object[] { 1, "DNT Mag", "1105 Media" }); public class Magazine
{
public Magazine(string name, string publisher)
{
Name=name;
Publisher=publisher;
MagazineId=Guid.NewGuid();
}
public Guid MagazineId { get; private set; }
public string Name { get; private set; }
public string Publisher { get; private set; }
public List<Article> Articles { get; set; }
} modelBuilder.Entity<Magazine>().HasData(new Magazine("DNT Mag", "1105 Media"); var mag1=new {MagazineId= new Guid("0483b59c-f7f8-4b21-b1df-5149fb57984e"), Name="DNT Mag", Publisher="1105 Media"};
modelBuilder.Entity<Magazine>().HasData(mag1); modelBuilder.Entity<Article>().HasData(new Article { ArticleId = 1, MagazineId = 1, Title = "EF Core 2.1 Query Types"}); var mag1=new {MagazineId= new Guid("0483b59c-f7f8-4b21-b1df-5149fb57984e"), Name="DNT Mag", Publisher="1105 Media"};
modelBuilder.Entity<Magazine>().HasData(mag1); public class Publisher
{
public string Name { get; set; }
public int YearFounded { get; set; }
}
public class Magazine
{
public int MagazineId { get; set; }
public string Name { get; set; }
public Publisher Publisher { get; set; }
public List<Article> Articles { get; set; }
} modelBuilder.Entity<Magazine>().HasData (new Magazine { MagazineId = 1, Name = "DNT Magazine" });
modelBuilder.Entity<Magazine>().OwnsOne (m => m.Publisher)
.HasData (new { Name = "1105 Media", YearFounded = 2006, MagazineId=1 }); migrationBuilder.InsertData(
table: "Magazines",
columns: new[] { "MagazineId", "Name", "Publisher_Name", "Publisher_YearFounded" },
values: new object[] { 1, "DNT Magazine", "1105 Media", 2006 }); namespace EFCoreMultipleDb.Web
{
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
applyPendingMigrations(app);
// ...
}
private static void applyPendingMigrations(IApplicationBuilder app)
{
var scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var uow = scope.ServiceProvider.GetService<IUnitOfWork>();
uow.Migrate();
}
}
}
} namespace EFCoreMultipleDb.DataLayer.SQLite.Context
{
public class SQLiteDbContext : DbContext, IUnitOfWork
{
// ...
public void Migrate()
{
this.Database.Migrate();
}
}
} public static void Main(string[] args)
{
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<yourDBContext>();
context.Database.Migrate();
}
host.Run();
} modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasField("_validatedUrl")
.UsePropertyAccessMode(PropertyAccessMode.Field); public class Blog
{
private string _validatedUrl;
public int BlogId { get; set; }
public string GetUrl()
{
return _validatedUrl;
}
public void SetUrl(string url)
{
using (var client = new HttpClient())
{
var response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
}
_validatedUrl = url;
}
} modelBuilder.UsePropertyAccessMode(PropertyAccessMode.Property);
namespace Loans.Models
{
public class Product
{
public Product()
{
Rating = new Rating();
}
public Rating Rating { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public double OfferPrice { get; set; }
public Group Group { get; set; }
public int GroupId { get; set; }
public List<Image> Images { get; set; }
}
public class Rating
{
public Rating()
{
}
public Rating(double totalRating, int totalRaters, double averageRating)
{
TotalRating = totalRating;
TotalRaters = totalRaters;
AverageRating = averageRating;
}
public double TotalRating { get; set; } = 0.0;
public int TotalRaters { get; set; } = 0;
public double AverageRating { get; set; } = 0.0;
}
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public Group ParentGroup { get; set; }
public int? ParentGroupId { get; set; }
public List<Group> ChildrenGroups { get; set; }
public List<Product> Products { get; set; }
public Image Image { get; set; }
}
public class Image
{
public Guid Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
public int? GroupId { get; set; }
public Product Product { get; set; }
public int? ProductId { get; set; }
}
} modelBuilder.Entity<Product>().OwnsOne(p => p.Rating)
"The entity of type 'Product' is sharing the table 'Products' with entities of type 'Rating ', but there is no entity of this type with the same key value ."
Owned entities are still entities. And owned entities are not optional when using table splitting (the default).
public double TotalRating { get ; set ; } = 0.0 ;
public int TotalRaters { get ; set ; } = 0 ;
public double AverageRating { get ; set ; } = 0.0 ; modelBuilder.Entity<Product>().OwnsOne(p = > p.Rating)