I'm weak on my fundamentals here, so feel free to let me know if I'm making any bizarre/false assumptions: I'm working on a project with WCF data services and due to the limitations of Odata as a querying language (e.g. no select distinct, joins are difficult, etc.), we're going to expose some service operations for common & non-trivial queries. So, I like using linqpad to get my linq code correct, and I'd like to use linqpad to target the entity framework directly (this is important: not through Odata). Is there a way I can target EF directly? Would it have to be on the same machine? What is the process?
Thanks!!!
You can target EF directly if you can point LINQPad to an assembly containing the typed ObjectContext that you created in Visual Studio. Click Add Connection and choose Entity Framework in the bottom listbox.
Related
I'm trying to create a simple Blazor client server app using EF, similar to this article.
So I've got a client, server, and common libraries, and this worked fine. But then I added the EF component to the common library, so that I could use real data from my database, instead of toy data from the demo.
I tried making them all Core 3.0, but this doesn't work because Blazor seems to require .NET Standard 2.0. Without that, I get all kinds of errors.
But then the common library can't use EF, because (if I'm reading this right) EF6 isn't supported on Standard 2.0. If I try, I again get tons of errors.
So I'm not sure, but I can't find any scenario that would allow me to share EF objects between client and server--which is a major rationale for Blazor.
Is there some other way to accomplish this?
The shared library should not use or reference EF.
Add EF to the Server project only and make the data available through an API controller.
You should make the common project netstandard and use EF core (not EF 6)
I am using Entity Framework (EF) 5.0, Code First approach, and SQL Server CE 4.0 database in my application. However, I am facing a major performance problem on application start-up.
I searched on the Internet and found this article which explains which operations affect start-up performance, and one of them is view generation. So, I looked into how I could generate views at compile time and link them to EF at run-time instead of creating views at run-time. I came across Entity Framework Power Tools which provides a command to generate views through your DbContext class.
I have generated the views at compile time using Entity Framework Power Tools as described in this article. However, when I run my application with SQL Server CE it always generates the following exception:
The mapping and metadata information for EntityContainer 'DatabaseContext' no longer matches the information used to create the pre-generated views.
whereas the same application works fine with SQL Server database. So, I searched more but have not found a fix to this problem. Following are links where people have reported similar performance problems:
MSDN Blogs
Entity Framework forum on MSDN
My question is this: "Is there a workaround or solution for this application start-up performance problem?". I need to use SQL Server CE and not SQL server.
Actually EF Power tools were generating the view using the default provider SqlClient but, I needed to generate the views for SqlServerCe.4.0 provider which, I was unable to figure out why every time generated views goes out of date and finally I figured out why it was not working.
So, I just commented the DbContext constructors from my context class and, then I ran the "Generate Views" and "View Entity Data Model XML" command of EF power Tools, then EF Power Tools took the connection string from App.config otherwise it uses a default connection string that connects to Sql Server with SqlClient as provider.
So, I believe that if anybody uses any other provider like Devart's Oracle provider etc., then he/she needs to generate the views by specifying the connection string and provider information in App.config (for desktop applications) or web.config (for web applications) and comment out the DbContext constructors (or hide it from EF Power Tools using per-processor).
I'm trying to conjure an approach to having a code-first like existance within a Windows Phone 7.x app combined with a Portable Class Library that houses the entities.
In that, rather than having my FooClass be decorated with [Table].[Column] attributes I'm keen to approach this from a different angle. I'm keen to have a method or an intercept/override that allows me to define the definition of my SQL Server CE tables before creation and also map them appropriately back to entity classes.
In Entity Framework via .NET 4.5 you can do this via the DbContext.OnModelCreating() method but I can't see anything like this for Windows Phone 7?
The closest thing I have found is CreateDatabaseSchemaUpdater() but that appears to be an approach post database(s) initial creation which won't work if you have Table<MyEntity> within your DataContext class (for later strong-typing usage elsewhere in your codebase).
Thoughts, Suggestions, Demo code?
Is there a template or tool to generate code from the database directly? I want to use model first scenario but do not want .edmx file for mappings. There is a database with many tables and I do not want to write all the classes (I am lazy) for that. So, is there a template to generate the code and set the annotations/use fluent api for defining the relationships, etc automatically from the existing database?
This would be helpful in the following scenario as well. Say, I was using .edmx with POCOs and now I do not want the mappings in the .edmx file. I want the mappings in the code. It would be great to have a tool or a template to generate the mappings in the code from the existing database.
I am starting on learning EF 4.1. I think "Code first becomes model first in version 2 i.e. after the database is created/released (in version 1) and needs some changes". Is that really true? I'd love to hear some comments. Thanks.
Check out the 'Reverse Engineer Code First' feature of the EF Power Tools CTP1 that was just released.
For generating classes, you can use POCO t4 template generation. Have a look at this detailed link which will help you getting started. That way you will get all the classes generated.
For mapping, you can use Code-Only style for Entity Framework but generating classes and context using POCO template will have far more advantage over creating the mapping yourself. Imagine adding new tables or modifying the tables, it will involve more work. But I will certainly love to know if there is any mapping tool for that.
It is possible that you are using EF-provider Devart dotConnect for Oracle when working with Oracle database. In this case the following information will help you to choose the tool.
The first version of Entity Framework Power Tools also contained the capability of generating a Code-First model with fluent mapping from an existing database. Although useful, this functionality is limited as regards its flexibility: the developer can only set the connection string; following that, classes are generated from all database objects available to the user. That is not extremely convenient, since in Oracle, for example, numerous schemas containing hundreds and sometimes thousands of tables are available to the user.
Rather than resort to this limited functionality, the users of Devart ADO.NET providers can avail themselves of impressively robust design-time development capabilities of Entity Developer, an EF-designer delivered with Devart providers. Also possible is the choice between the Database-First approach, as provided in EF Power Tools, and the Model-First approach, within which Code-First classes are created in the EDM-designer.
When compared to EF Power Tools, the Database-First approach to the development of EF Code-First models also allows selecting objects that must be available in the model, setting naming rules for the generation of class names and properties and so on. Besides, the resulting model can be modified and improved in the designer.
To better meet developers' needs, Code-First code generation in Entity Developer both for C# and VB is based on the T4-template that is easily accessible and can be modified in feature-rich T4 Editor contained in Entity Developer.
For more information on Code-First development in Entity Developer, see "Entity Developer – EF Code First DbContext Template"
http://www.devart.com/blogs/dotconnect/index.php/entity-developer-ef-code-first-dbcontext-template.html
I've been using the entity framework in combination with the self tracking entity code generation templates for my latest silverlight to WCF application. It's the first time I've used the entity framework in a real project and my hope was that I would save myself a lot of time and effort by being able to automatically update the whole data access layer of my project when my database schema changed.
Happily I've found that to be the case, updating my database schema by adding a new table, changing column names, adding new columns etc. etc. can be propagated to my business object classes by using the update from database option on the entity framework model.
Where I'm hurting is the CRUD operations within my WCF service in response to actions on my Silverlight client. I use the same self tracking entity framework business objects in my Silverlight app but I find I'm continually having to fight against problems such as foreign key associations not being handled correctly when updating an object or the change tracker getting confused about the state of an object at the Silverlight end and the data access operation within the WCF layer throwing a wobbly.
It's got to a point where I have now spent more time dealing with this quirks than I have on my previous project where I used Linq-to-SQL as the starting point for rolling my own business objects.
Is it just me being hopeless or is the self tracking entities approach something that should be avoided until it's more mature?
What version of self tracking entities are you using?
I'm using the .Net 4.0 version together with visual studio 2010. All CRUD operations work fine, also operation with FK.
I had problems in VS 2008 with FK but that's gone in VS 2010 with .Net 4.0.
If you want, I can provide you some samples.
Greetings
Since STE entity does not support lazy loading you should use Include on the server side include related properties. There is no way to include all related navigation properties. You have to explicitly include the properties. for instance
//server side
customer.Include("Orders.OrderDetails").Include("Address")