Entity Framework 4 cast object to dynamic proxy - entity-framework-4

In EF4, is it possible to convert a POCO object (created using new MyObject()) to a Dynamic Proxy (like you would get with ObjectContext.CreateObject())?
Using EF4 with T4 template for POCO.

No it is not possible. It is a reason why CreateObject method exists.

Related

Using same entity model from different assemblies

I creating a little modular asp.net MVC application. I am using dynamic entity framework for adding a entity model to dbcontext:
i have a category entity and i want to use this entity in different project in one solution. when i running application i see this error:
The type 'Module.Pages.Models.Category' and the type 'Module.Menus.Models.Category' both have the same simple name of 'Category' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model

Specify Connection string for Entity Framework used from multiple projects

I am currently using EntityFramework 4 with POCO objects. The POCO objects are located in their own .net project (project.Models). The Context is located in the DAL project (project.DAL). I have multiple other projects that I wish to use the context/models, for Example:
project.Website
project.Webservice
project.ConsoleApplication
Question:
How do I set the Context's connection string myself?
I have noticed that the Context object automatically finds the connection string in the web.config of the website if I add it there. Do I need to do something similiar for all the other projects? This seems inelegant and i think i'd rather have a way to manually configure it from my own config file or something.
Thanks!
AFrieze
You can pass a connection string as the first argument to the ObjectContext constructor. Read it from wherever you like and pass it explicitly, if that's what you want.

Entity Framework - Creating a strongly typed projection in the designer

I am trying to create a strongly typed projection entity "ProjectedPersons" via the designer in the Entity Framework.
I am using POCO classes and have seperated them out into seperate projects.
I keep getting an error:
error 3027: No mapping specified for the following EntitySet/AssociationSet - ProjectedPersons.
How can I remove this error. I only want to use the entity for projections not mapping to the database.
Thanks
If you are using the object only for projections - I would recommend creating it as a complex type instead. You get much of the same features without a lot of the contextual overhead that is inherent to an entity. I believe an entity is fully expecting to be mapped to a database feature in some respect - hence your error.
If you want to define projection in designer you must use QueryView (projection query defined directly in the designer and mapped to the new readonly entity) - that is the feature which must be manually defined directly in XML (MSL part) not in the designer. Otherwise you must do projection manually in Linq query. For linq query you don't need to create entity in the designer. You can use any class which is not mapped in the designer.
The QueryView was the solution. I found a helpful example.
http://msdn.microsoft.com/en-us/magazine/ee336312.aspx

Generate DataAnnotations with Fluent API and ObjectContext

I'm building an application using MVC 3 and Entity Framework 4.
I've created my Entity Data Model and generated a database from it.
Now I know the validation attributes such as [Required] or [StringLength(5)] can be used on the model properties to provide validation both clientside and serverside.
I would like to know if these attributes can also be generated dynamically instead of having to add them to the model explicitly? I saw that in EF 4.1 RC you can make use of the Fluent API to further configure your model in the OnModelCreating method by using the DbModelBuilder class.
As shown here
I'm working with a framework however that still uses ObjectContext instead of DbContext so I would like to know if the above solution can be used in combination with ObjectContext?
As a final note, since I've been trying to figure out how to generate and use data annotations it seems using view models would increase the complexity of validation. From what I read here it seems that just passing the models directly to the view would remove the need to add annotations to the models as well as the view models. However that means that you can no longer use strongly typed views when you do joins on the models and pass those to the view directly?
No it can't. Fluent API is different approach to describe mapping. You can use fluent API or EDMX (Entity Data Model). Not both. Fluent API also works only with DbContext API. If you want to have annotations generated you can try to modify T4 template generating your classes.
I have come across a disturbing issue when using poco classes that are extending base classes.
For example, let say you have a Person poco class that has a strongly typed Car property. You also have a Spouse poco that also uses the Car Property.
Now you want to display "Person Car" and "Spouses Car" in the view using the Display("Name = xxx") attribute. You cant!!! Becareful of this issue if you are not using flat View Models

ASP.NET MVC 2 Validation: Metadatatype can't be added to standard POCO CLR classes - what's an alternative?

I am using Entity Framework and generating my POCO classes via T4 - these classes inherit from nothing and are very plain and simple (created via template in vs 2010)
I tried using the Metadatatype Attribute so I could create a buddy class but when I did this I no longer was able to see my properties... if I removed the attribute! the properties appeared.
Anyway, searching deeper I found this statement from Microsoft
The associated class must be used with EDM or LINQ-to-SQL models because CLR types cannot mark existing properties with new attributes. If you are working with CLR objects directly, sometimes referred to as Plain Old CLR Object (POCO) types, you can apply the attributes directly to the model
So it appears it doesn't work. Anyway it's difficult for me to insert my Data Annotation on the MODEL itself because it's created via T4 hence if I edit it and then re-run the tool it will remove all my changes.
There is a pretty strong consensus around SO and the MVC blogosphere that you shouldn't annotate your business/crud/domain classes with attributes. Not only is your entire MVC stack becoming dependent upon your business/database classes but you'll quickly end up with multiple context scenarios ( same Model, different validation rules ) that are impossible to validate with just a single model.
Use separate view models for your screens, annotate those.
Based on your comment: "Data Annotation on the MODEL itself because its created via T4 hence"
What I'm trying to say is put your dataannotations on your viewmodels, leave your POCO models alone.

Resources