entity-framework-core

Entity Framework Core in Full .Net?

风流意气都作罢 提交于 2019-12-01 05:46:34
问题 Is There Any Way To Implement Entity Framework Core In Full .Net Framework Console Application? 回答1: First you need to create console application with full .net framework, Second install these packages using package manager console, Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools –Pre Now you need to create your model and context namespace ConsoleEfCore { class Program { static void Main(string[] args) { MyContext db = new MyContext(

What are expected results when optionsBuilder is defined in both DbContext.OnConfiguring and AspCore Startup.ConfigureServices?

好久不见. 提交于 2019-12-01 05:38:41
问题 My ASP.NET core has this class which gets called first public class Startup { public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<IssuerContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddMvc(); } And my context has this: public class IssuerContext : DbContext {

Is it possible to get complex Entity Framework objects from a REST api in .NET without creating ViewModel objects?

冷暖自知 提交于 2019-12-01 05:27:31
Imagine a set of Entity Framework entities: public class Country { public string CountryCode { get; set; } public string Name { get; set; } public string Flag { get; set; } } public class Market { public string CountryCode { get; set; } public virtual Country Country { get; set; } public int ProductID { get; set; } public virtual Product Product { get; set; } } public class Product { public int ProductID { get; set; } public string Name { get; set; } public virtual ICollection<Market> Markets{ get; set; } } Imagine as well a DOTNET 5 api GET // GET api/product [HttpGet] public async Task

how to apply common configuration to all entities in ef core

不羁的心 提交于 2019-12-01 05:01:49
问题 I have entities derived from a base entity in my application which uses ef core code-first approach. Base class public abstract class BaseEntity<T> : IEntity<T> { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public T Id { get; set; } object IEntity.Id { get { return Id; } set { } } private DateTime? createdOn; [DataType(DataType.DateTime)] public DateTime CreatedOn { get => createdOn ?? DateTime.Now; set => createdOn = value; } [DataType(DataType.DateTime)] public DateTime?

Override Default Identity Table Names

南楼画角 提交于 2019-12-01 04:48:27
问题 I would like to rename the default identity table names: AspNetRoles AspNetUserClaims AspNetUserLogins AspNetUserRoles AspNetUsers I understand how to do this using EF6: protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>() .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id"); modelBuilder.Entity<User>() .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User

How to include related tables in DbSet.Find()?

可紊 提交于 2019-12-01 04:40:27
问题 If I want to include related objects in an EF7 query, it's nice and easy: var myThing = db.MyThings .Include(t => t.RelatedThing) .Where(t => t.SomeCondition == true) .ToList(); Also, there's a nice method on the DbSet<T> that makes it easy to load a single object by its key: var myThing = db.MyThings.Find(thingId); But now I want to load myThing by its Id, along with its RelatedThing . Unfortunately (and understandably) .Find() is a method of DbSet<T> , not IQueryable<T> . Obviously I could

Loading of references in EF7

馋奶兔 提交于 2019-12-01 04:37:43
I'm having two classes - author and blogpost: public class Author { public Author() { Blogposts = new HashSet<Blogpost>(); } public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Blogpost> Blogposts { get; set; } } and public class Blogpost { public Blogpost() { } // Properties public int Id { get; set; } public string Text { get; set; } public int AuthorId { get; set; } public Author Author { get; set; } } Using EF7 (beta4), I'm connecting them the following way: public partial class MyDbContext : DbContext { public virtual DbSet<Author> Author { get; set; }

Entity Framework Core doesn't validate data when saving?

∥☆過路亽.° 提交于 2019-12-01 04:32:32
问题 I have the following remote validation rule: [AcceptVerbs("Get", "Post")] public IActionResult ValidateWindowEndDate(DateTime? endDate, DateTime? startDate) { int minWeeks = 8; if (startDate.HasValue && endDate.HasValue && (endDate < startDate.Value.AddDays(minWeeks * 7))) { return Json(data: $"Inspection window end date must be at least {minWeeks} weeks after start date."); } return Json(data: true); } That's linked to a property in my class as follows: [Required] [Remote(

Cannot provide password in Connection String for SQLite

亡梦爱人 提交于 2019-12-01 04:25:47
问题 I use SQLite with Entity Framework. I create the DB with following code: class MyContext : DbContext { // DbSets, OnModelCreating(), etc. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Data Source=c:\\test.db"); } } //this is in my test method using (var db = new MyContext()) { db.Database.EnsureCreated(); } The above code works. The DB gets created. But I want to encrypt the DB by providing password in connection string:

Is there a way to map complex types with EF Core

一笑奈何 提交于 2019-12-01 04:16:53
问题 EF Core does not support Complex types mapping. If I had an object such as: public class Entity { public string StringProp {get; set;} public SubEntity NestedEntity {get; set;} } where SubEntity is : public class SubEntity{ public int IntProp {get; set;} } How could I map this to a table that has columns for StringProp and IntProp. Basically one record in the table is composed of the properties of both Entity and SubEntity. I've tried ignoring SubEntity and exposing it's properties in Entity