background preloader

Code-First Development with Entity Framework 4

Code-First Development with Entity Framework 4
.NET 4 ships with a much improved version of Entity Framework (EF) – a data access library that lives in the System.Data.Entity namespace. When Entity Framework was first introduced with .NET 3.5 SP1, developers provided a lot of feedback on things they thought were incomplete with that first release. The SQL team did a good job of listening to this feedback, and really focused the EF that ships with .NET 4 on addressing it. Some of the big improvements in EF4 include: POCO Support: You can now define entities without requiring base classes or data persistence attributes.Lazy Loading Support: You can now load sub-objects of a model on demand instead of loading them up front.N-Tier Support and Self-Tracking Entities: Handle scenarios where entities flow across tiers or stateless web calls.Better SQL Generation and SPROC support: EF4 executes better SQL, and includes better integration with SPROCsAutomatic Pluralization Support: EF4 includes automatic pluralization support of tables (e.g.

EF 4.1 Released - ADO.NET team blog I get the following error after I try to add DbContext generator to the project: Error 1 Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Unable to locate file Server stack trace: at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path) at System.Runtime.Remoting.Messaging.StackBuilderSink. at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) --- End of inner exception stack trace --- at System.RuntimeMethodHandle. at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

Building N-Tier Apps with EF4 This article is the third in a series about n-tier programming with the Entity Framework (see msdn.microsoft.com/magazine/dd882522.aspx and msdn.microsoft.com/magazine/ee321569.aspx), specifically about building custom Web services with the Entity Framework (EF) and Windows Communication Foundation (WCF). (In some situations, a REST-based service or some other approach is appropriate, but in these articles, I’ve focused on custom Web services.) The first article described a number of important design considerations and antipatterns. While Simple Entities is usually not the preferred pattern for n-tier applications, it is the most viable option in the first release of the EF. The Entity Framework team has used these features to implement the Self-Tracking Entities pattern in a template, making that pattern a lot more accessible, and while DTOs still require the most work during initial implementation, this process is also easier with EF4. Figure 1 Comparing N-Tier Patterns with EF4

Mono WCF Advent Day 5: expose and consume WSDLs - monogatari WCF services can be exposed as a set of WSDL documents, and WCF client classes can be automatically generated by consuming such WSDLs. Today's topic is our WSDL support. It is not really perfect, but supporting code is there. ServiceDescription When you create a ServiceHost with your service implementation class, WCF creates an object of "ServiceDescription" that describes the service details, such as set of endpoints, debugging support and service throttling. As I explained Day 2 a bit, a service class may implement more than one service contract interfaces. WSDL output and ServiceMetadataBehavior To explain how to do it, I have to explain "service behaviors". WSDL output support is represented as one of such an implementation of IServiceBehavior, namely ServiceMetadataBehavior. Run service.exe, and then visit in your browser. svcutil.exe and clients On the client side, it is non-coding land. To use it, start service.exe first and then run:

Conventions for Code First - Entity Framework Design The latest preview of Code First allows you to describe a model using C# or VB.Net classes. The basic shape of the model is detected by convention and then a fluent API can be used to further refine your model. We recently posted about our plans to support Data Annotations as another way to further describe your model. We are now looking at extending and improving the conventions that initially infer the shape of the model. Conventions are designed to provide a starting point for a model. Previously Code First would infer that a property is a primary key if the property is called ‘Id’ or ‘<class name>Id’. When defining a relationship between two types it is common to include a navigation property on both types, such as in the following example: public class Product public int ProductId { get; set; } public string Name { get; set; } public Category Category { get; set; } public class Category public int CategoryId { get; set; } public ICollection<Product> Products { get; set; } public class Book

Self Tracking Entities on the server side - MergeOption.NoTracking a viable solution? Thanks for replying, I appreciate you taking the time. If you are on the server, I would rely on the object context to be responsible for changes. in your case, i think the delete is not recognized because, you are applying changes on an object context, from which it was retrieved from All of the STE functionality is of no consequence as long as the entity instance is connected to it's context, which is why calling MarkAsDeleted is basically a noop in the example. Although MergeOption would give u the desired result but you will run into this problem again once the entity is attached to the object context. That is true, but I would in fact never do that, since that would violate the Unit of Work pattern. One option you could do is check in the object context if it is tracking the object, then detach it and call applychanges. I think the problem with that would be that Detach only applies to the instance not the object graph. from your single method like

Get MVC3 Razor Running on Mono - Nathan Bridgewater Updated 10-18-2011 – added some more common issues I dug around a little to figure this out. Hopefully this will clarify all the steps required to get an MVC 3 Razor site running on Mono. The main pitfall I ran into was gathering the additional dependencies and excluding Microsoft.Web.Infrastructure assembly from my deployment. Overall though it’s not too difficult to deploy your MVC3 app. In Summary: Install MonoBIN Deploy WebsiteConfigure Apache (or Xsp environment)Fire Up Your ServerErrors You Might See Install Mono If you haven’t done this yet, go grab it from the Mono downloads page. BIN Deploy Your Website For this, I just performed a traditional “Publish” from Visual Studio to a file system directory. System.Web.MvcSystem.Web.HelpersSystem.Web.Routing BIN Deploy Additional MVC3/Razor Dependencies Just like Scott Hanselman mentions in his BIN deployment post, add these additional libraries found in the MVC3 RTM. NOTE: Do NOT copy the Microsoft.Web.Infrastructure assembly. #! Fire It Up

Querying Entities and Associations In this task, you will create strongly-typed queries against the CLR objects that represent entities and associations in the School model, and bind display controls to the object collections returned from these queries. To query the departments in the School database At the beginning of the code file for the CourseViewer form, add the following using (C#) or Imports (Visual Basic) statements to reference the model created from the School database and the entity namespace.Imports System.Data.Objects Imports System.Data.Objects.DataClasses using System.Data.Objects; using System.Data.Objects.DataClasses; At the top of the partial class definition for the CourseViewer form, add the following code that creates an ObjectContext instance.' Create an ObjectContext instance based on SchoolEntity. To display courses for the selected department Next Steps See Also Concepts Other Resources

Generating Business Logic “Hooks” for EF4 Entities - system.data.objects dev guy Once again a question in the EF MSDN Forum has prompted a blog post where I can give a more complete answer. If I understand things correctly, the person asking the question wanted a simple way to add business logic hooks to their entities which would be called whenever they did SaveChanges. In EF4 we made available the basic building blocks for this kind of thing, but unfortunately it’s not quite as easy and discoverable as it ought to be. Since I’m stuck sitting at home with my knee elevated as I recover from minor knee surgery, I thought I’d take a little time today to create something which should make this much easier. You can find the result here. It’s important to realize that this complements whatever other code generation you have going on—it doesn’t replace it. Once this is done you can write your own partial class for any of your entities and implement any or all of the OnAdded, OnModified and OnDeleted partial methods. - Danny

Dean Hume - ASP.net MVC HTML5 Toolkit If you haven't started playing around with HTML5 yet, you should definitely check it out. I've blogged about some of the features of HTML5 before, so if you take a look at this post, it should give you a fairly idea of what HTML5 is about. I try and play around with ASP.net MVC as much as possible and this site is also written in MVC 2. So, I've finally got around to creating a proper project and solution for these controls. Getting started Firstly, you need to add a reference in your project. Then, you simply call it the same way you would call a normal textbox in your MVC view. There is also added support for Strongly-Typed HTML Helpers And any additional html attributes: Another great thing about HTML5 is that it offers native support for placeholders - no more need for heavy javascript! You can also call the new Html5 Range and Number input types. And they display like so: If you currently use model validation in your controllers, you will be happy to know that this is also supported.

Entity Framework 4.1 and 4.2 The Entity Framework is a set of technologies that support the development of data-oriented software applications. The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications. For more information, see Introducing Entity Framework. Overview Entity Framework is an object-relational mapper (ORM) that reduces the impedance mismatch between the object-oriented world of .NET Framework developers and the world of relational databases. There are two major layers in an Entity Framework application: The modeling layer The object layer The modeling layer contains three components: See Also

Attaching Modified Entities in EF 4 Lately I’ve been working with EF 4.0 and finding that many of the new features are catching up with the features previously available in other framework like LINQ to SQL. One example of this is the ability to easily attach objects (for example, disconnected objects that come in from another tier). For example, imagine you had a web service where a consumer submitted an object to get saved – you’d want to instantiate a new context, attach the incoming entity, and save the object. In previous versions of EF, this was not a trivial thing to do. However, this was always quite easy to do in LINQ to SQL by doing this: 1: using (var dataContext = new MyDataContext()) 3: dataContext.Contacts.Attach(someEntity, true); 4: dataContext.SubmitChanges(); 3: dataContext.Contacts.Attach(contact); 4: dataContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 5: dataContext.SaveChanges(); 1: static class ContextExtensions 5: objectSet.Attach(entity); 11: foreach (var item in entities)

Introduction to HTML5 Development Using VS 2010, HTML5, jQuery Mobile and ASP.NET MVC 4 | Windows Store Apps - One Stop Shop You can download from Windows Store at Mobile apps and HTML5 are two of the hottest technologies right now, and there’s plenty of overlap. Web apps run in mobile browsers and can also be re-packaged as native apps on the various mobile platforms. Browser adoption for HTML5 features is accelerating rapidly. Many of the modern browsers (Chrome, Safari, Firefox, and Opera) already support between 75% and 90% of the feature set and this number is increasing daily. With the wide range of platforms to support, combined with the sheer power of mobile browsers, HTML5 is the “Write once, run anywhere” (WORA)solution. In this post we will learn how to create a mobile Web applications that take advantage of HTML5, jQuery Mobile and ASP.NET MVC 4, you can effectively target multiple platforms with a single code base. Tools Needed for development (download and install): Creating a HTML5 application:

Getting Started with the Entity Framework 4.1 and 4.2 Getting Started with the Entity Framework [This page is specific to the latest version of the Entity Framework. The latest version is available as the 'Entity Framework' NuGet package. The msdn.com/data/ef site is now the main location for the Entity Framework content. The content for this topic is now available on the following page: Get Started. See Also Concepts Other Resources Build Date: Show: © 2014 Microsoft.

Related: