Tagged with Entity Framework
If you're using .NET Core 3 and Entity Framework Core 3 together, you're probably using the EF Tools too. I've been running into an odd issue with it and wanted to share what is going on.
I know I don't look like it, but I actually like to follow the suggestions. So when I get the following suggestion form the command-line, I tried to update my version:
I've had a great time this week attending two events and talking about things I love: .NET and Vue.
Was a busy week, but really had fun time presenting again. Lots of great questions!
Earlier in the week I had the pleasure of speaking at DotNetSouth, a new event by the people who brought you Connect.Tech. They run a great show. I talked about .NET 3 (and 5) as well as Vue.js. Here are the slides and code:
I was reading my newest issue of MSDN Magazine and came across Julie Lerman's great article on how to configure Logging in Entity Framework Core. While this is great information, it only covered logging Entity Framework Core from a non-ASP.NET Core project so I figured I'd explain how to do it in ASP.NET Core.
The biggest thing that is different from the way that Julie shows this is that ASP.NET Core automatically wires up the ASP.NET Core logger to the context when it injects a context into your project. So to see Entity Framework Core logging, you need to enable it your ASP.NET Core logging, not in Entity Framework Core.
First let's talk about how Logging is handled in ASP.NET Core. In your
Program.cs file, the
WebHost.CreateDefaultBuilder() automatically sets up logging:
If you didn't notice, Entity Framework Core 2.1 has a new way to support seeding your databases with a method called HasData. Julie Lerman has a great new Data Points column in MSDN that explains how a lot of it works.
Go read that article first. It really covers the basics. Unfortunately, for my use, her article missed a tiny detail that I think is useful. But let's start with a brief overview of how HasData works.
In Entity Framework before .NET Core, entity framework had a way to create seed data but that method had a number of issues so they decided not to bring it over to Entity Framework Core. Now that we're into version 2.1 of Entity Framework Core, they wanted to allow for a way to seed the data with certain types of data.
I know I am not going to make everyone happy with this post. I've been hoping to not have to make this post, but Entity Framework Core has finally added support for Lazy Loading, so it's time.
This problem is not new. Entity Framework (not Core) also has this problem. But it's far easier to accidentally do this in that version. Luckily, Entity Framework Core has made it harder to inadvertently turn it on. Let's see what's wrong with Lazy Loading in Web Apps.
Just to make sure were talking about the same things, I'll explain how Lazy Loading works.
As many of you know, my recent course on Pluralsight dealt with Best Practices in ASP.NET: Entities, Validation and View Models. As I’ve worked with clients, there seem to be a non-ending list of ways to deal with data in ASP.NET.
One of the topics that I am passionate about as it relates to the course is how to manage the Model to Entity relationship. While being pragmatic is important, I still believe that there are many situations where you want a separate Model for a view (server or client-side) instead of just using the Entities that you’re storing data with.
The challenge here is how to do the mapping between the entities and models without having to churn out tons of boilerplate. Luckily, there are a number of solutions including AutoMapper.
For my upcoming course, I have a decent sized example that I’ll be teaching from. In the process of watching ASP.NET 5 go through the sprints, I have to upgrade the project at every step. I feel at some point I should be getting better at dealing with the sprints, but not yet ; )
Here is a short post that includes the different things I had to deal with in upgrading the project. It’s not just the ASP.NET 5 update, but also EF7 and a couple of small details.
First, I followed these instructions I found as an answer to an upgrade problem on SO. It got me close:
NOTE: This post has been updated for changes in Beta 7 and later.
Every web project needs some sort of data framework and ASP.NET 5 is no exception. Like it’s forbearers, ASP.NET 5 uses Entity Framework, but this version of the Entity Framework is different. It’s being re-engineered from the ground up just like the ASP.NET 5 stack.
In this, the third part of my ASP.NET 5 series, I’ll be talking about Entity Framework 7. While you can use older versions of EF or other storage mechanisms (NoSQL, Postgres, etc.), I think it’s an interesting exercise to see what the EF team is thinking in this evolving version of the framework.
In this second post in my six-part series on ASP.NET 5, we’ll take a look at how your ASP.NET 5 applications will be configured upon startup. The startup in this new version of ASP.NET 5 is very different, but hopefully is clearer and easier to debug. At least that’s my impression so far.
If you haven’t read the prior topics, it would probably be helpful to start with the earlier articles. You can see a list of the links to the articles below:
In this new course I build a new web site from scratch. I start out with a Bootstrap template (since my design skills suck) and move through creating content, building a database, exposing a REST-ful API and building a Single Page Application. I wrap it up by publishing the site to Azure Web Sites showing you how to not only get your application up an running in the cloud, but also how to monitor it and handle standard tasks like using your own domain in Azure.
You will see every line of code (with one small exception) that I write. This isn't slide-ware…it's show-n-tell. I use a range of technologies including:
As many of you may have heard, I recently launched http://GiveAQuiz.com as a new web site for creating and taking quizzes. The Data Team at Microsoft were great in helping me build this site. I've written a whitepaper detailing how we used the Microsoft data stack to accomplish it. This whitepaper shows how we used these data technologies to build the site:
As my continued facination with all things SQL Server Modeling related, I was tasked with writing a short article on the introduction of the basics of SQL Server Modeling for the developer. The result is the article "The Busy Developer's Guide to SQL Server Modeling" that was released on MSDN today. It is short so you can get the big details without investing a month learning the technology. Let me know what you think!
I like to write blog posts where I offer some pragmatic advice. In most posts I try to include tons of code samples and example projects...but this post is different. I am trying to get my head around something so I want to share what is in my head so I can get a conversation started with my readers to help me out. Once you read this post, please comment...
The other day I was responding to a tweet from Doug Purdy. He had posted a link to some new EF 4.0 features by the boss at Microsoft's DevDiv. I, as usual, complained instead of lauded the list. I started a conversation about lazy loading and the potential danger of it, but quickly Doug mentioned that ORM's maybe were the wrong approach in general. That got me thinking (not always a good idea).
I've finally had a chance to take a look at the July CTP of RIA Services. My opinion is mixed, but its still pretty early. I ran through the simple walkthrough and it was easy to set up but it still felt as if there is too much Visual Studio magic (a complaint I've had for a long time now).
My friend Jim Wooley has been doing some work with the Entity Framework team to help them get their examples into Visual Basic. The first of the fruits of these labors is now available! Hop on over to his blog to see all the details:
One of my favorite patterns in the Entity Framework is a smarter way to use discriminators for data in a database. Using Tables per Hierarchy (TPH) or Tables per Type (TPT) all for more complex modeling of data in a relational data store. In a simple example, you might have a database that looks like this:
Pardon the link post, but this is a great short walk-through of using LinqPad to execute EF queries against your own model. Great for testing query ideas against your data. Highly recommended.
One note is that while the AutoCompletion extension of LinqPad is great, it not necessary for this technique to work. Please buy AutoCompletion to support the great work in LinqPad, but it isn't necessary to get it to work.
If you missed me in Bulgaria's DevReach conference, the video of my Silverlight Data Access talk is now available. The original talk's description was:
In this session, we will explore the different methods for dealing with data in your Silverlight 2 applications including LINQ, ASMX, WCF, REST, Astoria and WebClient calls. The session covers both how to consume data as well as how to expose data to Silverlight 2.
UPDATE: Roger Jennings correctly stated, I meant to say that Include is *not* a guarantee.
When I am using the Entity Framework for a project, I have gotten into the habit of using eager loading via the Include syntax. In case you're not familiar it, the Entity Framework has a different philosophy than other data layers (e.g. NHibernate). In the Entity Framework, relationships have to be manually loaded when they are lazy loaded (so developers never have network round-trips without explicitly knowing about it). Whether you agree or not with this philosophy, understanding how it works is helpful when you're working with the Entity Framework.
I've finally had a chance to update my Silverlight 2-ADO.NET Data Services example. In this new sample I show how to create a Line-of-Business application (an XBox Game editor) using ADO.NET Data Services against both an Entity Framework model and NHibernate. Unlike earlier examples, this one includes implementation against the ADO.NET Data Service Silverlight 2 library to support saving of changed entities. In addition, I show some techniques for paging, retrieving simple types over an ADO.NET Data Service and full styling of the application. I hope to add support for Forms Authentication in the coming weeks.
Feel free to post replies with questions about the sample.
Data is a funny business. While at the moment I am spending a lot of time teaching Silverlight, my passion still lives in the data. I was brought up on Minisystems (Multi-user CP/M and the like) where you were dealing with something like a database (though we didn't have that as firm a concept as you might think). Later I did quite a lot of desktop database development starting with dBase II (yes, I am that old), Paradox, Clipper, FoxPro and even Access. That naturally led to client-server and N-Tier development. Throughout all the time its become exceptionally clear how much data matters to most applications.
But using databases can be difficult as there is an impedance mismatch with object-oriented development and the natural structure of data. The solution to this for many organizations has been to build data access and business object layers around the data. These layers were meant to simplify data access for most developers and embed the business logic directly into a re-usable set of code instead of it ending up in the UI layer. This was a good thing...
But the problem is that much of the data access (and to a lesser extent, business object) code was very redundant. Developers ended up writing the same code or simply mimicking schema that was in the database. Some started to develop ways to use the database schema to their advantage to limit the amount of hand-written code was created. While not always called this, this is where object-relational mapping (ORM) products got their start.
The Entity Framework "No Confidence Vote" is a couple of days old now. I wanted to give the Internet a couple of days to chew it over and figure out where it really fit into the big picture. If you follow me on Twitter you may have seen some back and forth between Scott Belware and I recently. Most of this back and forth has been about his attacks of the Microsoft community (attacks of the technology or even the company are fair game as far as I am concerned). Getting personal by accusing me, the Microsoft community or even individual EF Team member's directly seems petty and unnecessary.
In that same light, some of the ALT.NET community (I won't name names) have accused me of being a shill for Microsoft's techologies. Anyone who has ever seen me talk about any Microsoft technology already knows that I pride myself in my centrist view of any technology (Microsoft's or others).
For me the power of the Entity Framework is *not* in its use as a ORM. There are dozens if not hundreds of ORMs out there. Depending on your specific use case, some are great...some are less than great. None is a perfect ORM for every case. My attitude about the Entity Framework is that it isn't a great ORM...but that it isn't trying to be. The power of the Entity Framework is in a common language for communicating a model. The Entity Data Model can handle complex scenarios. The big advantage in using the Entity Framework comes when the Entity Data Model is used by Microsoft and external projects. The effort required in building a real model of existing enterprise data is not trivial. The labor involved can only be recouped if the model is useful in other scenarios as a model of your data throughout your organization. That's not the case yet. Microsoft isn't there. In addition, the Entity Framework is not a good tool for the top-down development that Domain Driven Design and Model Driven Design are espousing. Again, I don't think that was ever their intention. But its important to know. There are other criticisms of the technology to me made but that's not the purpose of this post. Instead its to say that its a great start on a long-term strategy and for many people now isn't yet the time to use the Entity Framework.
I've known Julie Lerman (or is it Julia these days ;) for a long time now. She's an excellent resource for everything data related. In particular she's been keeping up with the Entity Framework and ADO.NET Data Services (formerly Astoria) updates in .NET 3.5 and VS SP1 Beta that was just released this week. If you are upgrading projects (like I am), she has two excellent blog posts about how to upgrade your projects:
Over the past week there have been a flurry of new projects coming out of Microsoft, mostly in the form of CTP's. I've been downloading like crazy and will likely be discussing my experience with them in the coming week. In case you missed any of them:
I expect that with the release of the EF Beta 3 means Astoria is coming soon. I'll let you know when it drops! Time to start digging in.
Come see me and Glen Gordon talk at Phipps Plaza in Atlanta about Visual Studio 2008, "Astoria", Entity Framework and Silverlight...and see them working gracefully together!
Click here to register for the event before it fills up.