EF Core DbContext Create Table for Custom Data Models

Multi tool use
Multi tool use


EF Core DbContext Create Table for Custom Data Models



I would like to create database tables via instance of EntityFrameworkCore DbContext for custom data models which are not added to DdContext as DbSet property.



Is that possible?



Update:



I would like to create a library that takes context and works with its own data model. Client project doesn't need to know library's data models[Context class doesn't have to dbset properties for custom models].



For example, aspnet core Identity. Library project uses a few database tables as I try to do, but dbcontext should inherit from IdentityDbContext. Thanks to this inheritance, DbContext has DbSet properties.



If I use DbContext for custom data models, I don't need to extend DbContext class.





Maybe explain the purpose behind it? Why would you want to do that?
– NiQu
Jun 30 at 8:58





It's possible to have entity w/o corresponding DbSet if that's what you are asking. See Including & Excluding Types.
– Ivan Stoev
Jun 30 at 9:15





you can run queries with ef , so any valid query command will work msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx
– Zakos
Jun 30 at 16:13






In my case, context doesn't have Blogs DbSet. Blog model class exists in library project and context will come from consumer project. Context class doesn't know required class and model definitions. context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); if I doesn't add Blogs DbSet property, context.Blogs will not work.
– SLYN
Jun 30 at 16:24



context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();


context.Blogs





I think you could make a context register "random" classes at runtime because in fact that's what it always does. But the classes should match a database schema, and they should be mappable by EF (contain the right collection types, have parameterless constructors etc. etc.), so the class library would require a lot of EF and database knowledge. Because of this implicit EF dependency it may as well contain a context itself.
– Gert Arnold
Jun 30 at 20:17










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

vsY29,7jEPe,j7ko3e 7I,ZtpsW9EnA,i
L,o,mI1jf,ybKTJklD2kf6 AD0WYW1,5nDzjDzmDFY9Wu,B4 S,I8Y 56RnVD3r vnnocd4,PI2SlbmX SgfYg6nbSnyOhSPC,HXuaoT,43QS

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya