Visual Studio Toolbox

Entity Framework Core

Entity Framework Core
5 (100%) 1 vote
(Video Transcript)
Hi, welcome to Visual Studio Toolbox. I’m your host Robert Greene and joining me today is Julie Lerman. Hey Julie.

>> Hi, Robert.

>> Welcome back on the show.

>> Great to be here and we’re in the big studio this time.

>> I know, isn’t it awesome.

>> Yeah, yeah it’s nice.

>> And we’re gonna talk about some Entity Framework today.

>> Yeah. And a little fun with Visual Studio 2017 at the same time, just cuz I can’t help myself.

>> Wow. [LAUGH]

>> But, Entity Framework Core.

>> Entity Framework Core.

>> Yeah.

>> Okay. So, for the listeners or the viewers who are still confused on the difference, Entity Framework, Entity Framework Core. What’s going on?

>> We’ve been working with Entity Framework through Entity Framework 6. Entity Framework Core is the new version, but it’s not Entity Framework 7. Although, they did originally name it that for a short while. It’s really a lightweight cross platform version of Entity Framework. So, Entity frameworks is going to continue on it’s path, although the real innovation will go in the EF Core. Entity Frameworks 6 will continue to be paid attention to, and updated, and take full requests, and bugs fixed, and all of that stuff. And with all its big rich features and as Rowan Miller has said on Channel 9 before, it is battle tested.

>> [LAUGH]

>> And EF Core is new. So, EF Core can be used with just full .NET Framework but it also has the ability to go along with .Net Core and be cross platform.

>> Okay. So, if you’re already building an app using Entity Framework, you should continue using it.

>> No reason to toss out everything and start from scratch.

>> No, no.

>> There rarely is.

>> And it’s not just an update, as any framework has been in the past.

>> Okay. And if you’re building with .NET Core, specifically, ASP.NET Core or a multi-platform application, you would certainly use Entity Framework Core, because it’s designed to work very well with that. Right?

>> Yes. And also, with UWP. Okay.

>> An interesting thing is if you’re using ASP.NET Core because you really love the features of ASP.NET Core, but you totally plan only to deploy to Windows, so you don’t need the cross-platform stuff. In that case, if you really want to, if you already have maybe APIs, you’ve written with the EF6, then it is possible to mix those. All right, so you can have EF6 with ASP.NET Core, like just in their own assemblies.

>> So, you could use Entity Framework Core from a .NET or what was that?

>> Well, that’s backwards, no, yeah, from for 6 but, that’s stiff, so, you can use EF6 with ASP.NET Core.

>> Right.

>> You can use EF Core in full .NET Windows only applications, as long as they’re I think 4.52 were above.

>> All right.

>> So WPF, right? Windows Forms, ASP.NET Web Forms, right? As long as you got that framework But you can also use EF Core if you’re doing full-blown, .NET Core targeting cross-platform.

>> Right, okay.

>> [LAUGH]

>> Well, it’s important to be really, really

>> Yeah.

>> Really clear about that.

>> Right. That make sense. All right. So, we’re gonna talk about Entity Framework Core today.

>> Correct.

>> Excellent. All right.

>> And a little bit of .NET Core.

>> All right.

>> And a little bit of .NET. And a little bit of everything, actually. And that’s the cool thing about EF Core, because you can use it on Windows and you can also use it anywhere that .NET Core runs. So we’re gonna be kind of all over the place.

>> All right.

>> So, the first thing I actually want to show you, because I am excited to be using the latest release candidate of Visual Studio 2017, along with the new tooling. You must of had these conversations also about how when you’re focused on .NET Core, the project JSON support is shifting back to MS Build and CS project. So, people who’ve been already working with .NET Core, and EF Core, and using projects where the metadata, so to speak, the project the project metadata is in project Jason.´

>> Mm-hm.

>> If, in order to move up to these newer building tools, there’s ways to migrate projects. So of course I had to check that out. I’ve got solutions that have .NET Core and EF Core in them, and using Project JSON. And I wanted to bring them up. So, I’m gonna take the same project and I’m gonna migrate it two different ways.

>> Okay.

>> Okay. So, first I just have to grab the project and first I have to see. So, I don’t mind looking at you and you’re blurry but the computer screen I got to see. I got to see.

>> It’s my best side.

>> Yeah. So, this is a project that it’s an EF Core, ASP.NET Core project, that I build in Visual Studio 2015. And if I go in there, into this source, you can see evidence of that because I have xproj and I have project.json.

>> Okay.

>> Okay. So, now I’m like, I wanna use the current, the coming, it will be current at some point soon, so I wanna move to that, but I’ve got this Project JSON, how do I turn it in to a CS proj file? So, the migration tools do that. So, the first thing I’m gonna do, I’m gonna take this project, I’m gonna copy it, I’m on a Mac, you may not be able to tell, but you may be able to tell. So, I’m gonna copy this into a shared folder on this computer. So, here’s my shared folder and I’m just gonna paste this. So, there’s the XProj. Now, I’m gonna go back over, now I’m back over in Mac. So I’m moving back and forth from being another sex. Which is where I am now. Over here, I’m in a virtual machine.

>> Right

>> That has Windows 10 running on it. And so, that’s also there. So, here’s the new XProj folder with all the stuff I just showed you. So, what I’m gonna do, is I’m gonna go right out to the command line. I’m gonna use .NET Core command line tools. And I’m using the newest command line tools, and this is actually easier on Windows cause I can just type the cmd on the toolbar, which is something I only learned only a week ago.

>> [LAUGH]

>> Did you know you can do that? There was a big Twitter storm about everybody going my God, I didn’t know you can do that. I was one of them. So, anyway I had to go out to services and new terminal tab at the floor. So, here I am now in, I’m just gonna assume, no, not that way. You’re gonna edit this part out.

>> We don’t edit on this show.

>> You’re lying.

>> [LAUGH] Actually, I’m not.

>> Okay, I can’t remember.

>> Anything.

>> All right, I was gonna increase my screen, my font. And I haven’t done that in so long, cuz I’m really truly a Windows developer.

>> [LAUGH]

>> So, I’m still a little dorky on my Mac. Anyway, so here I am in that project. And there you can see some of that evidence. There’s the source and my Samurai tests and everything. I’m gonna go into the source, actually no, my solution file is here. There it is.

>> Yep.

>> My solution file is here so I can do this right here. So, just first of all, I wanna show you, I have .NET running, and I’m just gonna do now dotnet –version. So, you can see that the version, this isn’t the version of .NET, this is the version of the SDK that I’m using.

>> Okay.

>> So it’s Preview 4. Okay. And if I say, dotnet help, there’s a few new commands in the tools, the .NET CLI tools.

>> It’s migrate.

>> That we didn’t have before. One of them is migrate. So, I’m gonna clear the screen. Wow, I’ve really cleared the screen.

>> [LAUGH]

>> Here it is. And now, I’m gonna say dotnet migrate. Now, what’s gonna happen, it’s going to see the solution folder. Read it. Find all the projects that are in the solution. Find all of their, these are .NET core right, so they all have Project JSON, right, they don’t have CS Projects, right.

>> Right. So, it’s going to find all that stuff and it’s gonna convert it to use in this stuff. And I just can go a cover, really, really fast cover., something, right. So, it has now migrated the projects, so.

>> Now, again if you so if you haven’t Done any Core projects, and you started today with the latest build, you don’t have to do any of this stuff.

>> That’s right.

>> Because we’re already back to csproj.

>> Right.

>> Which means if you’ve never done it before, nothing’s ever changed and you don’t know what we’re talking about. Is that right? So there’s an important thing to know about that, which is, that won’t work, you can’t use the new stuff in Visual Studio 2015. There’s not gonna be tooling in Visual Studio 2015 to use that new stuff. So if you want to use this newer stuff, you’ll either move to Visual Studio 2017, and there’s the Community Edition. Or Visual Studio Code, right, or Visual Studio for Mac. Right. Right, so all of those will be able to support the new tooling. Okay. But Visual Studio 2015 won’t. And a matter of fact-

>> But 2015 would continue to support some version-

>> Project JSON. Of .NET Core? Project-

>> The previous version. Yes, yeah, with project.json. All right, fair enough. And you could do that with Visual Studio Code too. If you target the older SDKs. Cuz you wouldn’t be able to open the same project in both 2015 and 2017. No, as a matter of fact, it depends on how things are structured in the csproj file. Okay. All right, so, I’m not going to dwell too much on that. Yeah. I’m going to show you some of this, just cuz this is kind of cool. Yeah, I don’t want to hijack-

>> And to take away the fear. Well, that’s why I want to show it to you, it’s interesting, right? Yeah [LAUGH]. But take away that fear, or especially, for me. I’ve been using EF Core, I’ve been using project.json, now I’m gonna be moving to the new stuff. So even though I’m not an ASP.NET Core person, I still have to deal with this. Right. So anybody else who’s in my shoes is gonna be helpful to see that. But if you’re brand-new to anything Core, then you just File > New Project and go and everything’s fine. Yeah, yeah. Okay. So while I’m on the Mac, I’m just gonna open this project up in Visual Studio Code. And I can do that by typing code, space, dot. And it’ll just open what I have here. Just so we can take a quick look at what we got. Open that up. So. It created these csproj files for my different projects. So here’s the Data project, and it created a csproj file, it’s XML now. Right? And here are how the package references are. Right, actually, that one becomes redundant because of the tooling.

We have this up here, this SDK, so we don’t even need that. Again, [LAUGH] it’s all fascinating to me. I wanna explain it all. [LAUGH] But the point is, project.json’s not here anymore. It took all of what it needed. You can see, there’s my references to my other projects. There’s my Entity Framework Core references. So it’s fixed all that up. And it did create a backup folder and tucked the project.json files away. Just the stuff that they got rid of, they tucked that away. So you have access to that. And then presumably if there’s errors, it would show you errors somewhere. But there weren’t. Of course not. Cuz it was so easy. Yeah, all right, so now back over to. This thing’s almost ready to go. Yeah. Of course there aren’t errors. Now, okay, I’m still on the Mac. [LAUGH] All these desktops, okay. Back to Windows, here I am in Windows. So, remember, this was the one I just copied on to the shared drive. I don’t want that one anymore. So I still have this one, the original one that I copied, right?

And this is the one, again, came from Visual Studio 2015, there’s my solution file, my source code. And there’s project.json and xproj. Right. Okay, so what I’m gonna do here is, I’m gonna open up a new instance of Visual Studio 2017 RC. And that one won’t take a few minutes. It says that, but it’s fast. It can take a few minutes. And I got three instances running now in a virtual machine. Wow. So that’s pretty nice. So now what I’m gonna do is I’m just gonna plain old open up the old solution that came from the Visual Studio 2015, that has all the project.json stuff. Uh-oh. I have to find it. I know it’s on C. I put it on an easy place to find. C, migrate tests. There it is. Phew. [LAUGH] Okay. So there is that, right, that’s the solution. I’m not faking you out. That’s still the same one we were looking at. So I’ll go ahead and open that. No faking allowed on this show. I don’t know about the other shows. Okay, so there is, woah, we, notice something, right?

It’s a one-way upgrade [LAUGH]. Think that’s very, very clear. It discovered all four projects that are in there with our xproj. And it’s just saying, we’re gonna go ahead and do this. And there will be a log file, all right? So it’s gonna do the same thing you just did in a-

>> Right, and I didn’t even have to know that it’s gonna do it.

>> Right.

>> When I was in the CLI, I needed to know I needed to migrate.

>> Right.

>> I needed to know I wanted to migrate. Here I’m just like, I’ve got a new version of Visual Studio, yay. Right, I wanna keep working on my .NET Core projects that I’ve been working on. I’m gonna open them up. And it’s just gonna go, we’re just gonna do this little thing for you, don’t worry about it. And now you’ll be able to use it in here, plus you get the new tooling. So here it is. Do you trust it? Is it a trustworthy source? Did you write it? Because it came from the Internet. This is opening up the log. Now, there is one note here, that it had to make some changes, non-functional changes. And rather than reading it, it’s just basically saying that it made some different references in the solution file to the new csproj files. Now, there are also some warnings here. And this one is just because my project was not using the most latest, greatest, greatest, greatest Entity Framework Core tools. And it’s just saying, you had Preview 4 and we’re gonna give you Preview 4 Final.

And all those tools are still in-

>> Churn, right.

>> Churn, so that’s fine. But the other interesting thing is that the csproj files are smarter than the project.json. In project.json, I had to explicitly say, I’m using .NET Standard 1.6.1. Here it’s saying, you have that package reference, but you actually don’t need it. Because with the new format, we have this .NET SDK tag on there. And that will go ahead and pick the right thing up.

>> Okay.

>> All right, but at this point, it’s just saying that, but it didn’t do anything about it. But I could just go in there and modify that. And that’s all the other error messages. They’re not even errors, they’re just warnings.

>> Right.

>> It’s in there, it’s basically gonna get ignored. Right, because we already put something in there that’s going to take care of it. So, woah, we’re in Visual Studio Toolbox. So, Toolbox just wanted to show. [LAUGH] Couldn’t help itself. Very appropriate. Yeah. Why are you all here? Just go like that. Thank you. So here it is. Now, here’s something else pretty cool. So, I’ve been using project.json. Before .NET Core, if I wanted to look at what’s going on, what references or anything are in a .NET project.

>> Mm-hm.

>> I would expand References. It’s actually listed a little differently in here. Instead of just References-

>> Or you’d exit, you’d go out to Windows Explorer, and open up the csproj file in Notepad or something.

>> Right, or I would just look at what references I have.

>> Yeah.

>> Now what you’re seeing here is that it’s actually making it easy to differentiate what we’re getting from NuGet. And what we’re getting from project references within the solution. So that’s why that looks a little different. But with that project.json, even though here now I can use Package Manager Console. I can use Add Reference and get that UI. I can go out to the Package Manager Console or I can just use Package Manager. I just want to edit. I liked it. I really liked it. And it gave you IntelliSense and the latest versions of libraries and whatnot. They should add that to Visual Studio.

>> Well, look at this.

>> Whoa!

>> So what did you have to do if you wanted to edit a csproj before?

>> Right-

>> You had to unload the project or go out and do all the stuff. Now you can edit the csproj.

>> Wouldn’t it be cool if you could just right-click and edit? Yeah, there you go. Now, right now, you can only do it in .NET Core projects. Once I started doing this in the .NET Core projects, And then I open up Visual Studio 2017 to work in .NET framework projects. I go and I right click and it’s not there. I’m like, no.

>> [LAUGH]

>> So we might get some more parity along those lines. All right, so it’s the .NET. Yes, it’s like I learned something and may I have to unlearn it. But I’d liked being able to do this.

>> And this is editable, I assume.

>> We don’t have all the IntelliSense yet.

>> Okay.

>> So it’s really frustrating. And I know that there’s a lot of, and I participated in it, I’m sorry, a lot of moaning and groaning when we were told sorry. We know that you loved Project JSON, but we’re going back to CS Proj 4. Very good reasons. Because I really liked that. [INAUDIBLE] So having looked at this and knowing the that IntelliSense and the help of the package reference is coming. They’re just working on it still. I’m okay with it because they’ve actually streamlined CS proj. Enormously, like that’s it, that’s all that’s in there, really streamline. So, here’s that SDK attribute up here, right, that makes this one a necessary.

>> Right.

>> Right, we’ll work all that out and maybe eventually the tool will go, yeah, I don’t need that, I’ll get rid of it or comment it out. So just in case you really explicitly want it for a different reason. So it just did all of that for me. So now I can take my .NET core projects that I was working on in Visual Studio 2015, project JSON and very easily move up to being able to use the new tooling.

>> Cool.

>> And EntityFrameworkCore. [LAUGH] Came along for the ride.

>> Yes.

>> Right? Well, it’s important to me because I’m using EF core so I’m using all that stuff also.

>> Right.

>> So I’m gonna close this project and then what I wanna do is just show you some cool things about Entity Framework Core.

>> Okay.

>> And, yeah my idea of cool things might not be the same as everybody else’s. So, this isn’t gonna be like the standard, not necessarily the standard features that everybody always highlights because we can see those. We get exposed to those a lot so as I’m learning things a lot.


>> Not everybody knows

>> I did do that. That’s really cool, right? That makes my life a little easier. So even though I have this numbered, I’m gonna go against my ADD grain. It’s gonna be a little hard but I’m gonna do things in a kinda different order here. So Entity Framework Core has a lot of familiar things from EF6, the basic ideas, we got the DB context and change tracking and DB sets and in a lot of the interaction.

>> Yes, should be very much the same. It does.

>> But something is going away totally. We’re not the ADMX anymore which is some people are like so what? I never use it. I was first. But there are a people have been using the designer and really depending on it. So there’s actually third-party support for EDMX and being able to work in a designer way. But this EF Core isn’t going to support it directly. So, the first thing I wanna show you that’s new in and cruel that maybe go, ouch. Is the fact that any framework nail supports mapping to fields. So in order for entity framework to comprehend the model and more importantly, in order for entity framework to be able to populate classes. After you’ve done a query and it’s populating objects, creating objects with the results. It was able to map to scalars, properties that were scalars and properties that were collect like for navigation property was a collection, so this was a problem for people who really like, I do a lot of domain driven design. And I’m really a control freak about how my classes are designed and how people, how other developers or myself even are able to interact with the classes.

And encapsulating fields and protecting them so that they can only be used the way I want them to be used, builds and properties. Anything in my class is really important. So one of the ways to achieve that when you’re not thinking about Entity Framework is to expose the fields and then, you can encapsulate the properties and interact with that. But Entity Framework wasn’t able to map to fields. It could only map to properties and eye collections.

>> Okay.

>> Okay? So, there is a way, if you just have a scalar property. There are ways around it. But we can now map to a backing field. So, I don’t have before and after here. I just have how it is new. So, for example, I, in this Samurai, and I have to give you the origin story of this domain when any frame works 6 came out. I kept calling it the ninja edition

>> Right

>> And then when EF core was originally called EF7, it made sense to me to go to Samurai because we first ninja the Samurai and also the movie The Seven Samurai which is a favorite movie of mine. So, now that its called the EF core, I’m just sticking with the Samurais, for now. So that’s the origin story of my, my Samurai domain. And so since its a movie, we have quotes, right. So, what I wanna do here in terms of encapsulation and saying I don’t want people to just willy nilly modified properties, if they wanna modified properties or set values to properties they have to do it under my rules. All right I’m setting the rules. So, you’re allowed to specify the name of the Samurai when you first create a Samurai, when you instantiate one. But Notice I’ve named to this immutable name just to make really really clear. So there’s the name property. Right?

>> Yeah.

>> And all the name property does is return this variable. Right? So you can’t say immutable name equals Robert.

>> You can’t code against that. The only you know I’ve said the only way you can set a name is when you are first substantiating this. So this what I’m able to do now is map to that backing field. An Entity Framework can, actually does have conventions that will discover things by itself. So for example, if this was just called name and then it couldn’t, but there was no setter here. It would see this _name and the convention would be able to assume that that’s what it should be mapping to. But there’s a way to, because I have something different, you can specify, so I’m gonna go over to the context, we’ve got fluent API, this is the wrong one. I got a few of them, there. I’ve got the fluent API, and I’m saying here specifically the property called the ImmutableName has a field called _name.

>> Okay.

>> So now Entity Framework will comprehend all the way through it from migrations, creating the database, understanding how to interpret link queries into SQL, and understanding how to take results and push them into objects so it will understand it. Now, I wouldn’t have to do this if the property was also called name because the condition would, the convention would figure that out.

>> Okay.

>> So that’s actually something we had a way to get around this before. Even though we couldn’t map to the field, there was ways to do that. But what we couldn’t do. Where’s my Samurai? What we couldn’t do is protect the collection. That was really frustrating, so you could add in methods here to say. Hey, if you wanna add a quote to here.

>> Uh-huh.

>> You have to go through here because I might have some other important business logic inside of AddQuote to make sure you’re doing it the way I, the things that I need to make sure of are happening. There was no way to do that. One of the reasons was originally, up until now, Entity Framework does not recognize. So this is a standard coding way for getting Entity Framework mappings again.

>> A standard way to do that protection is to make the property IEnumerable and then nobody can edit an IEnumerable. They can’t add another sword or add another quote into that IEnumerable. But, Entity Framework didn’t recognize that. Entity Framework did not comprehend IEnumerables and could not map to them, and now it can.

>> Cool.

>> So now I can protect this, so I can make the swords property navigation property IEenumerable which means any framework when it do queries any framework will be overpopulated but that’s the problem, right, but at the same time I’m able to stop anybody from adding and removing from there without using my methods that I’ve defined. My control freak methods, cuz I’m a control freak. So there is. I’ve learned this last little bit From a great blog post by Arthur Vickers, who’s on the team. He’s got a great blog at And he’s been blogging a lot of really good, detailed stuff about EF Core. One of the problems with this is it’s still possible for somebody to kind of work around this. Get back call sorts and then get the results of this, which will be this. And then cast it as a list and then change, they can do all that kind of stuff. So, there’s another way, and this is gonna get smoother to do in I think the next version of EF Core. So this is EF Core 1.1.

So there’s this cool trick of what’s referred to as a defensive copy. This was something new to me. I guess that’s, once again, because I was a history major, not a comp sci major. But have you ever heard of a defensive copy?

>> I was an economics major, I didn’t take comp sci either.

>> [LAUGH] All right, so a defensive copy is a way of returning a copy of an object, instead of the object itself. To make sure that nobody touches the actual object, okay. So here’s an example, so here I’m using IEnumerable, right? And I’m gonna return quotes any time somebody asks for quotes. I’m gonna return _quotes.

>> Okay.

>> But instead of returning _quotes, I’m calling ToList on it. So that’s actually creating yet another object.

>> Right.

>> So even if they do muck with it, they’re not going to really muck with my own sorts, okay. To do this you have to add Some code here, right? So this is this property, SetPropertyAccessCode. Notice I’m not even using Fluent API. I’m going all the way into the Metadata to do this, okay? So that’s what will get smoother. There will be a Fluent API way to do this. So this is more a matter of find my demos or find Arthur’s blog post and copy and paste to get that working for now. But what this does is even if I didn’t have this. And I ran migrations and created the database and inserted data into the database, all that would work. It would still find everything. This is about getting the data back. And so if you do a query, I could run it. I actually have real code here, I could show you the difference. So, here’s just a little tiny program that instantiates my context. I’m just tossing and recreating the database. Because it’s a demo, I’m creating a new Samurai. I’m adding a quote.

I’m from Vermont, so, eat more kale, is a good quote. I’m adding a new sword and giving it some value. Adding this graph now to the context and calling Save Changes And what I want to do is show, if I take out this code for now, when I go and run this. So I save the changes. And I say, okay it’s saved. The changes are saved. And I spin up the context again. And I do an eager load of that Samurai. Give me the Samurai, the first Samurai, all of that Samurai’s quotes and all of that Samurai’s swords. And then I iterate through, tell me the name of the Samurai, show me the text from all the quotes, there’s only one. Show me the weight of all the sorts, there’s only one, right. So if I run this, I don’t trust my Ctrl + S on the Mac keyboard [LAUGH]-

>> [LAUGH]

>> when I’m in Windows. Sometimes they just do different things.

>> So if I run this. Ad I’m just gonna do DEBUG > Start Without Debugging and spit it out to the console. So the changes were saved. Notice I didn’t give any quotes. Quotes is the one that I protected with that defensive copy. But I did get the swords. So that’s showing me that the IEnumerable mapping on its own is working without having done the super special thing, right? So just to get back to the program. Right, I added, right, this was the quotes. If I go back over to the Samurai. The swords, we saw the sword. The swords is the one that I just did the plain old fancy new IEnumerable mapping. But this is the one that I was doing the defensive copy. So in order for the defensive copy to work, I do have to specify this in the context. The data was in there. I’ve actually looked, opened up. My gosh, I think you’ve done a show with Eric who does the SQLite and SQL CE Toolbox?

>> Mm-hm.

>> It already works in this version of Visual Studio 2017 RC. So I was using SQLite. So I looked and the data definitely got in there. So when this was commented out it wasn’t preventing the model from being correct or the data from getting into the database. It was just preventing the query from populating the data with the results. So the results do come back, but it just can’t get them into the class. So now that I’ve uncommented it, and I guess I’ve saved it, I’ll just run it again. And this time I should see the sword and the quote.

>> Cool.

>> Okay, did you have your kale today?

>> Now, I’ve not had kale today

>> Sorry, couldn’t help it. Anyways, so, in my typically nerdy self of, my God, this is the coolest feature. I could just talk about this one feature all day cuz I think it’s so cool. [LAUGH] So there that is, so that all comes under the guise of field mappings. But for me the beauty is I can now finally truly encapsulate collections in my classes. I’m really happy about that, just in case you didn’t notice. Here’s something. I actually looked, I knew I had put issue on CodePlex when EF5 was on CodePlex and EF6 was on CodePlex. But I think I’ve looked this up. This is something I asked for five years ago. It’s been in here since the first released of EF1 but I didn’t realize it was there. [LAUGH] I finally discovered it one day. I was kind trolling through the source code and I went, what? So the ask, as you say at Microsoft, what’s your ask? My desire was to be able to define a dbcontext as read-only. The reason for that is when you’re doing queries with Entity Framework.

By default Entity Framework keeps track of all those results. It assumes that’s what you want to do, is keep track of the results. So in order to do that, as it’s pulling the queries and pulling the data back and creating your objects. It also creates these other little change tracking objects and keeps track of the state. And that’s where it knows it’s been added or updated or deleted. And when you call save changes what should be sent to the database. Okay, so there are plenty of scenarios where you don’t need that. You’re not gonna be using it, whether it’s read only. But more typically if you’ve got a web API or a website or something

>> Mm-hm.

>> And once you’ve made that request, the dbcontext goes away. It’s just you’re using the dbcontext on the server to do a query. It’s sending the results down the wire to your UI or whatever. And the dbcontext instance, all of that stuff goes away. The request, the dbcontext, all of that stuff goes away. So in essence, what it’s doing is you’re querying the data. It’s creating, spending all of this time, all of these resources creating those ChangeTracking objects. Maybe not so much depending on what you’re doing obviously. And then throwing it away.

>> Right.

>> Right. So what we’ve always been able to do in one form or another. The terminology changed as we evolved through different versions of entity framework. Was to be able to say, hey, this query that I’m doing, I don’t need you to create those objects. And the way we do that is we append, let’s see if I got a query here. Here’s a query. So we could always do that. We could say Asnotracking.

>> Okay, yep.

>> Right, so we could say, that query. Don’t track, don’t create those little tracking objects, right? But, you have to remember to do it, all the time. And if I’m creating a web API, I wanna do that for every single query. And I have to remember to do that for every single query. So I said, can’t I just say it for the DbContext? And now we can.

>> Cool.

>> Long story, right?

>> [LAUGH]

>> But I wanted to give you some context. So this actually, I could show it to you here if I’m debugging. So I’ll show you what the default is. So I’m just gonna run this again, and I’ve got a breakpoint here. But this time I’ll debug it. Hopefully I won’t have any other breakpoints. Okay, great, it’s at the end. So what I’m gonna do is look at the context itself to see what it’s tracking. And I’ll go to QuickWatch and context > ChangeTracker. And I’m just trying to find the most efficient way to see it. State manager. The state manager is what keeps track of those state objects. So it’s got three, right. For all the things, right, it just assumed that I wanted them.

>> Right.

>> So what I can do now, rather than, well, that was just one query, really. So it is kind of easy to do it there. But the point is, I don’t have to want to have to remember to do it on my queries. So I can go back to my context and I can do it any time, actually. So I can literally define, anytime you use this context. ChangeTracker.QueryTrackingBehavior equals, got two options, the default, which is TrackAll, and NoTracking, all right? So I can say just blanket this Xamarin context, no matter what, unless you say otherwise, we’ll never track.

>> Can you turn it on for an individual query?

>> You can, because now we have as tracking.

>> Okay, cool.

>> On the query, so.

>> So this is changing the default for any query that is done in this context.

>> On this one. I could even, maybe I don’t want it to be blanket for this whole context. Maybe I wanna say, I’m using this context in a few different places.

>> Right.

>> And in some places I needed to track. When I’m using it here, this place, I’m using it here. I don’t want it to track, so I could specify it on the, well, I was specifying it on the instance before, but I could specify it on this instance. So that’s the ChangeTracker.QueryTrackingBehavior, I could do it there.

>> Okay.

>> Okay, so I just want you to see the difference. Remember when I debugged it.

>> Yep.

>> Before, because I know you believe me.

>> [LAUGH]

>> I know. I can always get away with that. You believe me, don’t you?

>> Trust but verify, right?

>> Yeah. I have to see it. I don’t believe me. I always have to see. This is how I know how things work. I like to know, I like to look and really see what’s going on under the cover. So going back down into the ChangeTracker. Into the Non-Public members. Into the StateManager. And! I took it out. Remember when I.

>> [LAUGH].

>> I took it out. Did you see me do that?

>> See, that’s the downside to proving things.

>> Look. Okay.

>> You discover that [INAUDIBLE]-

>> That you’re a dumb dumb. [LAUGH] That’s what I usually discover.

>> Your worry, not mine [LAUGH].

>> I’m a little bit of a dumb dumb. [LAUGH] Right. Yeah, and this is the show where you don’t edit. You’re not gonna cut that part out and make me look better?

>> No way.

>> That’s when you’re talking to [CROSSTALK] and you tell them a dirty joke. And they’re like, don’t worry, it’s gonna get cut out. Or you’re like, you say something like, that’s not for public consumption. Well, [LAUGH].

>> The one thing we cut on this show is if.

>> If I do swear accidentally?

>> If you swear accidentally or if I swear accidentally. Or if you said something you weren’t allowed to say, like ship dates or something. Or if the demo was so completely horked up that the-

>> How’s this going so far?

>> Fantastic.

>> Phew, okay. So there-

>> Or if my phone rings, because-

>> You didn’t turn it off?

>> For the longest time, and I’ve told this story before, so bear with me. For the longest time, I thought it would be cool if my phone rang during a taping, and then it did.

>> Cool in what way [LAUGH]?

>> It’s funny.

>> [LAUGH]

>> And then so, I left it in, I thought that was funny. And then it happened again, and it was sort of humorous. Now, if it happens again, it’s just annoying, so.

>> Right, leave your phone at the door.

>> No, actually, it’s still in my pocket, and I haven’t turned it off, but nobody calls me.

>> Anyway, so, ta-da, there it is. Nobody calls you. I’m sure people call you. All right, what else is exciting? The in-memory provider for testing. That’s really, really cool. Are you familiar with this at all?

>> I’ve heard about it, but refresh my memory [LAUGH].

>> Okay, so the in-memory provider. So, have you ever written unit tests?

>> Sure.

>> Yeah. Have you ever written a unit test on something that really isn’t a unit test, because somewhere in there it’s making a database call?

>> I have written unit test for things that make database calls, yeah.

>> Great. And you’re going, why you say that’s not really a unit test? That’s a difference, that distinction is a different conversation. Anyway, so one of the things. Sometimes you really want to test what’s happening in the database. There’s no question about it. But sometimes, you’re testing something else. Maybe you just want to make sure that your web API, your controller method’s working, right. And somewhere in that controller method, it’s doing a save or it’s doing a query. And that is not really the point of what it is that you’re trying to test. Right, so whether or not it actually interacted with the database is not really the point. Or sometimes, what if something’s wrong with the database and then the test fails for the wrong reasons?

>> So you can use mocking frameworks, or you could interfaces or whatever.

>> Or, yeah, absolutely. So mocking frameworks.

>> There’s other ways of dealing with it.

>> Yeah, so we’ve enabled the EF-

>> Or you can just say, trust me, it works [LAUGH].

>> We’ll let the users find out if it works or not. [LAUGH] Three jobs later. Three careers later. So, so you’re learning good guidance here, too. Things you shouldn’t do in your career. So we’ve had the ability to build fakes since the earlier days of Entity Framework. I think EF4, we were able to start doing that. It was hard, and there was a lot of stuff to implement. Then with EF6, they made it easier to do mocking. But still setting up mocking, there is a lot of setup to do.

>> Yeah.

>> Right. So what they’ve done, they as in the Entity Framework team, was they created in addition to the SQL Server provider, and they also built the SQLite provider. And there are also third-party providers, like there’s one for Postgres and there’s one for SQL CE and everything. But they created an in-memory provider. So it’s not a database. But you point to it, you tell, instead of saying, the provider that I want you to use, Entity Framework, is SQL Server, right. Or in this here, you’re saying, the provider I want you to use is just going to happen in memory.

>> Okay.

>> Right, so it’s really lightweight. You don’t have to depend on a database. And it’s so easy to use, cuz it kinda does a lot of the magical stuff on its own. But it’s not mocking. So let me just show you what it looks like. So first of all, if you’ve got a project that’s tests, you don’t need to reference SQL Server, right? You don’t even need to make references to those things. So what I’ve done here is just, the package I installed is the EntityFrameworkCore.InMemory.

>> Okay.

>> Right, so I don’t have SQL Server in here at all. So I have unit tests. Now, you see, probably, you may not have noticed it a lot. There’s a number of ways to let the context know what provider you’re using, and if it’s a database, what the string is. I just hardcoded it in here for demo purposes, all right. So it’s just hardcoded. But another way to do it is to pass in the information about the provider and the connection string. That goes to, for a DbContext in Entity Framework Core, that comes to the context by way of something called DbContextOptions. So here’s a DbContext, here are the options I want you to use. The option, for example, the provider, the connection string, like if it’s a database provider. So I’m now going to be focusing on this context. It’s just a little different. The one I had for the mappings was set up so I could really show you the mappings. So this is a little more generic. So the thing to notice about this is I have two constructors here.

I still have to have a parameterless constructor for Entity Framework Core. But I also have a constructor that takes in a set of those DbContextOptions. So I can pass them in so I don’t have to define it upfront. Although if you look down here I’m doing something. Again it was just in order, it was in a way to show something else about EF Core. What I’m doing here is back to how I was kinda hard-coding the provider and connection string in earlier. I’m hard-coding them in here, but I’m giving myself a way. So, this is the default, the SQL Server provider. I’m using SQL Server and here’s my database. That’s my default. But notice here this, and this is part of the dbcontext, this is not my own method. So, it takes in an options builder. And notice this is configured. So I’m saying, if the optionsBuilder’s coming to me already configured, use that. But if not, this is a not, if not, use this. That’s how I get the default. So I’m giving myself here a way to pass in pre-configured optionsBuilder.

So over in my test, that’s what I’ve done here. I’ve created, sorry about the highlights. We need them but not when you’re trying to do a demo. [LAUGH] You’re like, go away, right? So what I’m doing here is I’m instantiating a builder all on my own.

>> Yup.

>> Right, and I’m saying since I have a reference here to the in memory provider. This is an extension method, right? UseSqlServer is an extension method that comes by way of the SQL Server provider being installed, right? And so there are other ones. So I have that installed. So I can say UseInMemoryDatabase. So InMemoryDatabase doesn’t need a connection string, so that’s all it needs to know. So, now this is a builder. And once you have a builder then you can pass in its options. And so that’s what I’m doing, I’m instantiating the context. And I’m passing in the builder options. So now, the context, when I get there. I can debug it, so, you can see, cuz debugging is so much more interesting. So I’ll just debug it so you can watch what happens. Actually, I want to run it first. So now you see how I’m letting it know to use this InMemory provider. So to do this all in memory without a database. So, now that I’m doing that, I’m just writing regular old code. I’m creating a bunch of Samurais and using context.Samurais.AddRange.

So I can add all of those. I’m saving the changes. I’m spinning, and now if I just said after Save Changes, okay, context, how many Samurais do I have? And it says three. That would not prove anything because it’s just looking in that context. So, I’m spinning up a completely new instance of the context. And saying, go do a query against my in-memory provider and tell me what’s in there. So it’s persisted.

>> Right.

>> But it’s persisted in memory.

>> Okay.

>> Without a lot of extra crazy code. So what I’m doing in this assertion is-

>> Persisted for the session of Visual Studio.

>> Yes, the application lifetime.

>> Okay.

>> And what I want to do after I show you this is flip over because that actually creates some problems-

>> The application lifetime.

>> Well, application lifetime, so the definition of application lifetime is a little different. When you’re talking about tests, running a test runner.

>> Okay.

>> Right?

>> So if you ran that again, would those three samurai be in there?

>> I’ll run it through first and then we’ll talk about it. So what I’m asserting is just that the three samurais are there. I should have made seven. It would have sounded better.

>> [LAUGH]

>> And the other thing is just to prove that, not only do I just have three samurais. I even have samurais with those names, right? So it’s gonna pass and then it’s gonna spit that out. So, I’ll just run this, not there, a couple ways I can do it but I’ll do it from here. And then I’m gonna go to the Test Explorer. There’s the Test Explorer so, it passed. And there are actually some cool new things about testing in Visual Studio 2017 but I’m not gonna show it. And it’s showing me the ID, right? The second thing, that was kind of stupid, what I did with the outline. So it passed and it said, yes, I’ve got three samurais. And then I said, tell me the ID of the Samurai named Samson. And that was 2. So it’s definitely doing that. Now it’s gone.

>> Okay.

>> Right, so if I ran it again and I didn’t do this part.

>> Right.

>> That would fail, right?

>> Okay.

>> It’s gone, I’m done, the application instance isn’t running.

>> So, it’s application life time.

>> Yeah.

>> Cool.

>> However, that creates some problems. How are we doing for time cuz I could just go on and on.

>> Yeah, we’re getting close to an hour.

>> Okay. Well this is actually important because I’ve really had to, why is that. Maybe I won’t be showing it.

>> [LAUGH]

>> Hello. Hello. I’m just gonna, okay that’s good, that’s happy. Going back over to this other instance. That’s not happy. Yeah, that’s weird, isn’t it. That’s cuz I’ve just been doing too many weird things. All right, let me just open it up again. Yeah, not responding. That’s probably just me. Right?

>> It is still pre-released software.

>> Yeah, it’s an RC, And we got to do this too, bang on it, bang on it, bang on it, do weird stuff. And believe me, they’ve gotten some nice dump files for me.

>> [LAUGH]

>> [LAUGH] Okay. So, here some more tests, this is a constructor here. Here’s just another series of tests in here. So this is just different little solutions. So, actually this solution that I migrated, right? Say that was the solution that I was working in. So, I have my domain classes, I’ve got my context in here. This isn’t the solution I migrated. Look, this is entity framework core in just plain old .NET, no .NET Core. It’s a wpf app.

>> Okay.

>> Yeah, but that’s not what we’re doing here. We’re in here for the test, in it for the test. So here are the tests right here. So I’m doing a few things here. My first test is one that really does interact with the database. Now normally if I have integration tests I really wanna hit the database. I’m not gonna have them in the same test class as the lightweight test. Cuz this could be a slow and cumbersome test, and it will slow everything down. But I just have them all in here, just cuz I just wanna show you all this quickly, quickly in my way that is. So, this is a test just to prove that the database interaction is working as I expect it to do. So I am using SQL Server, right? There’s my SQL Server connection string. I am getting rid of it and recruiting it. Cuz it’s a test, I want a fresh database so I know what I’m working with. [INAUDIBLE] on here. Create a new Samurai. But I’m not gonna focus on what it is I’m testing. The bottom line of what I’m doing is we’re creating a new Samurai.

I’m inserting it into the database. And then I’m checking to make sure that I know that the database is gonna be assigning an ID. So I’m checking to make sure that whatever is the ID of the Samurai after I’ve done the insert is not whatever it was beforehand. That the database really stepped in there and did something on the way. So, that’s what that test is doing, and it’s starting out doing, again, SQL Server. I have another test. Now, on this test I am not hitting the database, I’m still creating a new samurai. I’m inserting it and I’m saving changes. But this time I’m just using the in-memory provider. Now, see that I’m passing in in-memory options. I just defined them up above.

>> Right.

>> Just to get that a little cleaner. That’s just defined in the constructor.

>> Okay.

>> So I’m just passing them in. I didn’t use them before though. I used the other. So that’s going to use the in memory and I’m doing what I did before. I’m doing it on one instance of the context and then doing it again on the other instance of the context. So I’m basically kind of proving that I can save stuff. However I set up my dbcontext, that just basic functionality of being able to save stuff and Be able to do a query is working. So, my DbContext is set up correctly. That’s not really testing my business logic even. But I need to know that I set up Db, I set up EF correctly. And I also have a little repository in here. And it’s not like a repository pattern classic. It’s just I’m calling it that. It’s just a class that has methods in it. There are the methods I know I need. That’s called the ConnectedData class. It has a method for creating a new samurai.

>> Mm-hm.

>> It has a method for bringing back all the samurai’s that EF is aware of. Querying and loading a graph, calling safe changes, and then, I also have all this logic for battle. Tbis is actually something I pulled out of my EF Core course. So, building all that up in and I talk about all that. So, what I’m doing is I’m on a test something against here, right? So, I’m testing, I believe my test is testing a combination of CreateNewSamurai and SaveChanges which is down here. So, I wanna make sure that my ConnectedData methods are working. Not so much focused on, are they interacting with the database?

>> Right.

>> I just wanna make sure this logic is all doing what it’s suppose to be doing. So, I really don’t care about the database. So, this is a really good reason to be using in memory. So, that’s what I’m doing here. I’m creating a context within memory options, right?

>> Mm-hm.

>> I’m building the Samurai, creating that little, that repository, that ConnectedData class and passing the context in that uses the in-memory options. Calling its two methods, CreateNewSamurai and SaveChanges. Getting rid of the context, instantiating the context again, and making sure that I have the Samurais. So, I’m going to run this three different times for reasons, for someone who filled it. There they are, so, I’m just gonna run all of them. All right, CanInsertSamuraiIntoDatabase, that’s one that, that’s running. That’s the one that’s interacting with SQL Server.

>> Mm-hm.

>> It’s deleting SQL Server, it’s recreating the database. It’s a taking a long time.

>> Mm-hm.

>> It’s dinner time, I forgot, happy hour. Shoo, 22 seconds. So, two things here that I don’t like. A, that one took 22 seconds.

>> Yeah.

>> B, [LAUGH] one of my tests failed. Let me show you, this test that failed, CanInsertSamuraiViaRepo, let me just run it all by itself Passes. The reason-

>> What?

>> It didn’t pass before was because in memory is retaining the objects for the lifetime of the application.

>> Okay.

>> So, it actually, this and the CanInsertSamuraiWithSavedChanges and CanInsertSamuraiViaRepo ended up using the same instance of in-memory. So, when I got to CanInsertSamuraiViaRepo, there was already one in there. This one was already in there. Then, I added this new one and I said, the count should be one, but the count was two.

>> Yeah, okay.

>> All right? So, this is important, right? This is the [LAUGH] kinda thing like, I was like, why is this, why is this, why is this? So, what you need to do is, if you might want this all to kind of chain together and have dependencies. In that case, you should also control the order of them, that’s the way to do ordered tests, right?

>> Right, okay.

>> But I really want them to be independent. I want this one to have a fresh in-memory context and the other one. So, what I’m doing back up in my constructor. Remember, I said I was creating the in-memory options?

>> Yep.

>> The other thing I’m doing is I’m using the EF Cores DbContext database, ensure deleted method to make sure that. Now, this is interesting too. This is one of those things like people who do a lot of testing would know this. And this, I still don’t understand this. Even though it’s like the same instance, this constructor is getting hit each time.

>> Right.

>> I don’t understand that, but the constructor is getting hit each time I’m, it’s running a new test method. So, by deleting that in-memory database in the constructor, it’s gonna be fresh for each one of the methods.

>> So, each one has essentially it’s own instance of the in-memory database?

>> Yes, even though, yeah.

>> Okay.

>> Because I’m not creating a new instance of the in-memory database here. All I’m doing is creating a new instance of the context.

>> Right.

>> Right, so, that other code is forcing the in-memory database to get deleted.

>> Okay, so EnsureDeleted creates creates a brand new copy of the database.

>> No.

>> No?

>> It deletes it.

>> It just deletes it.

>> All right, and then, this will create it for me.

>> Okay.

>> It’ll go, I need that.

>> Okay.

>> The other thing I’m doing, remember I said I hated that how long this thing took. So in my test I actually have a few thing a few providers installed. I also have the Sqlite provider installed. So I just wanted you to see how easy it is to for test this week. Unless you test Unless you’re doing something that really depends on the behavior of a particular database.

>> Mm-hm. I can in most cases just as easily use SQLite,

>> Right. For those tests instead. Right, so now I have fixed those problems, I will run all the tests again. And, so the one in the database was less than the second Yep. But the ones that use in memory are even faster.

>> Yeah.

>> Like that’s pretty cool.

>> So that’s a pretty cool trick right in and of itself there [INAUDIBLE]

>> Well, it shouldn’t be a trick. [LAUGH]

>> Well.

>> It should be right. Right, but it is. Is like you know.

>> Create a SQL light database that has you know similar structure, contain just tested.

>> I don’t have to, because. I do one thing. I-

>> Cuz I’m a SQLLite database is persisted.

>> Yes.

>> So if you want persistence but you don’t wanna take a long time, then is SQLLite a good option?

>> Yeah, the

>> The other thing to take into consideration, my model was so simple that the migrations are, They’re okay. I don’t have a problem with the differences between them. So that is something you need to consider, if you’re switching From one database to another, you probably need to create a new set of migrations for-

>> Yeah.

>> That-

>> Okay.

>> Provider, and run those, if you’re gonna use those.

>> Still, if you’re gonna save-

>> But this is-

>> 20 seconds per test, it would be worth it.

>> Yeah. So this is not an uncommon trick, actually, to say, I wanna do unit testing. But the database is in the way, but I don’t wanna-

>> Right.

>> Maybe in my provider can solve every problem.

>> Why? You’re offline. You don’t have access to the database.

>> Right. Then maybe we’ll make provider. But using something SQL white Is something people have been doing with it instead of faking, instead of marking they’ve been just they are like just use simple SQL line, just do it that way. Anyway so those are the kinds of problems that I run into that’s the much the database is taking a long time. But not really comprehending about that in-memory database is kinda sticking around longer. You know, I thought it was gonna create a new one, right? And it wasn’t. So I thought that was an important pattern to show. So, and all of that in Visual studio twenty seventeen, can I show you one more thing? Of course. Such a good guy, so patient with your nerdy friends. So, I have not done, everything I have done so far has been Then, in full. .NET Framework.

>> Mm-hm

>> So, I do have a project here which is using .NET Core. And this is the one where you can tell because I can edit the .csproj. [LAUGH] That’s how you can tell it’s a .NET Core Project. Okay, so this one’s using SQL Server. And everything and I wanted to this a couple of things I want to point out Visual Studio 2017 if you’re using Entity Framework EF Core and inner core project You have the option, and you want to do migration, you have the option of doing migrations through PowerShell, the way we’ve always done them. Add -migration and update-database. Or you can use the command line. Line tools. Either way, that’s if you’re using .NET Core. If you’re just using plain .NET, you have to use PowerShell because it doesn’t know anything about .NET Core.

>> Right.

>> But I do have the option. But what the team did was The CLI commands look different, cuz the way the .NET commands are, .NET something at the command line, are different than the PowerShell commands. When you run them, then they point to the same, you can’t see that back there. Here are the commands, here are the API that actually does the migration. So when you run the commands, whether you use the PowerShell words or the .NET words, they call the same APIs, right? It’s the same thing. But what the team did was along the lines that have really defined .NET Core and EF Core being lightweight. And not putting everything in your packages, everything in your assembly just in case you need it. They split the commands for CLI into a different package than the commands for PowerShell. So it took me a while to realize this. Like, actually really an easy way to know. The difference is that the commands are in Microsoft.EntityFrameworkCore.Tools. Tools, plain old Tools will give you PowerShell.

Tools.DotNet will give you the CLI commands.

>> Okay.

>> All right? So, because when you see them, you’re like which one’s which? How come I can’t do this?

>> Right.

>> How come I can’t do that? So I can go out to the command line from here. Do you know this? This is one of Mads Kristensen’s extensions.

>> I have seen that, yeah.

>> Do you know, and I know he does a lot of extensions. Maybe you talk to him a lot. He’s got 79 extensions in Visual Studio Gallery.

>> I know, and I think we’ve covered 50 of them on this show.

>> [LAUGH] He’s amazing.

>> This, I think, I actually think this one should be built into Visual Studio 2017. Because so many people are going to be using Visual Studio 2017 for working with .NET Core, and they’re gonna want to go out to the command line, like me. I could go out to just a regular command line, which is really ugly because of that long prompt. You can’t get rid of it in just regular DOS command line. Is it still called DOS? So I’m going to go out to PowerShell, not because it gives me different functionality. What I’m doing in CLI is absolutely no different. I’m going out to PowerShell only so that I can say, function prompt {>}. That’s all. That’s the only reason I’m using PowerShell for this, cuz the other stuff just gets in the way. So I’m sorry it’s so dark. You think that’s gonna?

>> Yeah, it’s fine.

>> That looks okay. So, I always like to just make sure .NET is working, before I go any further. And it says that I’ve got 1.1, it says that up there at the top. Then, clear it again, and then dotnet ef, cross my fingers. That’s working. Did you know they put the little unicorn there?

>> There’s a unicorn. I think I saw that awhile ago, yeah.

>> Isn’t that the cutest? So notice, so I’m using Entity Framework 1.1. But this, again you’re seeing the tools version.

>> Right.

>> Right? So the Tools version I have here is 1.0.0-msbuild3-final. Very specific. Because the other ones, the preview ones, for Entity Framework. Entity Framework Tools packages that say preview are not going to understand the msbuild, csproj stuff. They were focused on, they were built for project.json. So I’m using this later, so that name of the tooling. And it’s confusing, cuz it’s Entity Framework 1.1, right? This is not Entity Framework, this is just what tools am I using to talk to Entity Framework. And that’s the version of the tools. So there’s subcommands, database, dbcontext, and migrations. There’s a couple of new ones that have gotten added in. dotnet ef dbcontext, I like this, this is brand new, info. So this reads the project and finds and tells me the name of the database and the data source. Isn’t that cool? That’s kind of new. So I’ll just clear this again, dotnet ef. So I can do migrations from here, migrations, and then after that there’s subcommands.

One of them is add and then, I can type in the name, I probably already have one, I might already have one in there called init. If I do it would be like, what? No, so it says it’s done and you can remove it if you want. So if you haven’t seen EF Core migrations before, it does the same thing as migrations have always done in response. So it created this Migrations folder. And it created that init, it created that. This is really a big deal. This snapshot file is a representation of the fact that Entity Framework Core now lets us do migrations in, when you’re within source control. Because it used to be that this snapshot that describes the model would be stored in your database. Right, and if you’re working on a team that was crazy. So now the snapshot is stored in the file, in this file and it’s just part of your source control. So that’s huge, for people haven’t dealt with that, they’re like, why, who cares? People who have dealt with it will be going fainting on the floor in joy, right.

Like it was a big big deal. So you can, there with database, you can database update. You can script, create scripts. All the same kinds of things that we’ve been able to do before but we can, that’s part of. I can do this, well not in PowerShell, but I can do this anywhere that .NET Core runs. Right, so I’ve done tons of demos of, which is why I own this pretty little machine. I’ve done a ton of Entity Framework Core demos in OS X using Visual Studio code, right? And it all just works the same, cuz the EF APIs are the same no matter where I use them. The APIs work across platform, but they’re the same whether I’m using targeting Windows or targeting cross platform. So, and that’s my story and I’m sticking to it until a new version comes out.

>> Cool. Awesome stuff. Awesome stuff. Thanks.

>> Yeah. Well thanks. It was great to do.

>> Lots of great stuff.

>> Yeah, we tried to do it a little while ago but we weren’t able to.

>> It was 75 minutes but it went, seemed like it was faster.

>> Good. Yeah, good. We were originally gonna do this in November during the summit, but we wouldn’t have had the latest and greatest version of Visual Studio 2017 to use. So thanks.

>> Thanks for coming on.

>> Yeah, lot of fun.

>> All right. Lots of good Entity Framework stuff there, I hope you enjoyed that and hope you stuck around for all of it. And we will see you next time on Visual Studio Toolbox.

>> Bye.


Read the video

In this episode, Robert is joined by Julie Lerman, who takes us on a deep-dive tour of many of the features that are new in Entity Framework Core.

Julie covers:

  • Overview of Entity Framework Core [00:35]
  • Migrating an existing .NET Core project that uses project.json to use the new MSBuild based tooling [04:12]
  • Field mappings and encapsulated collections [23:20]
  • Turning change tracking off for a DBContext [36:45]
  • In-Memory provider for unit testing [44:55]
  • Using the .NET Core CLI for migrations [1:08:40]

Comments to Entity Framework Core

  • Pretty cool session. Learnt something new (a)

    dubem February 2, 2017 5:32 pm Reply
  • do you know when it’ll be subtitled or provide a transcript?

    robcube February 10, 2017 1:48 pm Reply
  • I’ve added some quick EF Core walk though videos on Channel 9 that you can follow along with if you like. You can find them on my "niners" page at

    julielerman March 12, 2017 12:03 pm Reply
  • hiI can see the CC and translation to several idioms, but why isn’t Spanish ?the same happen with the other videos regards

    ltuttini March 16, 2017 6:49 am Reply
  • Very cool. I am especially interested in the improvements they are doing in EF core with the mapping to a private fields. What normally should happen with EF mapped entity classes is that they are mapped to Domain Classes which contain the Domain knowledge about the specific thing. I think that should still remain as we do not want modifying directly EF class but Business Domain class, and then after that mapping to Entity. But with the new idea of having private field – this will allow to hide some internal knowledge of the Entity to its implementation. That could be good but also bad thing. Bad because allows Business Logic to be in the Domain but also in the Entities. Wondering if EF team want to remove the gap between those two things and Entity class to have the most intimate knowledge about the domain…. ? What do you think?

    velchev March 17, 2017 2:14 am Reply
  • @ltuttini:I brought this up with some channel 9 people. I don’t think they realized this and they are looking into it now! thanks for pointing it out!!:)

    julielerman March 21, 2017 3:33 pm Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

1Code.Blog - Your #1 Code Blog