Some questions about the .NET Entity Framework and Stored Procedures
Hey everyone, I开发者_JS百科 had a couple of questions relating to the .NET Entity Framework and using stored procedures. Here goes:
I know that we can right click a stored procedure and choose Function Import to be able to use with code. Is there a way to do this for many stored procedures at once?
When doing a Function Import, I can create a new Complex type or use an existing Complex type. Well, how can I access Complex types/objects that are outside of the edmx file? That is, if I have a class in my project, is it possible to access it while doing a Function Import?
When calling the stored procedure from code, it returns an IEnumerable of the Complex type I set it as. However, sometimes these complex types do not have all of the properties that I need, so I create a new class in my project that inherits from the complex type used in the stored procedure. Problem is, I can't seem to cast the complex type returned from the stored procedure to the new class I created. Any reason why I can't do this? What I ended up doing is looping through the IEnumerable and adding each item to a new list of the class that I created. But this feels and looks messy.
Bara
Maybe I'm misunderstanding, but in your model you can
right-click
>Update Model from Database
. In theAdd
tab, simply select all the stored procedures you want to add to the model.Not as far as I know. The point of Entity Framework is to avoid having to create your entities manually (code generation). You either create your database first and copy to model or create model first and generate database tables. Either way, the actual .NET entities are generated automatically. If you later change your model, the entities are re-generated. If you could import your own pre-existing entities, you would risk having them overwritten when your model changes. They would also be outside the model's control (how to handle if they're in separate assemblies, etc?). With partial entities (more on that on #3), you can extend the generated classes without risking your customizations getting overwritten. You could still use your existing classes as DTOs, but then you would have to manually convert from the EF-generated entity to your DTO object.
You can't cast a base class to a derived class. See: A way of casting a base type to a derived type
I'm not sure about .NET 3.5 (EF 1), but entities/complex types in .NET 4 (EF 2) are partial classes. That means you can add members simply by extending the class. So for example, if your custom type was:
public partial class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
You could create a new file with:
public partial class Address
{
public string MyProperty { get; set;}
}
Then do new Address { Address1 = "abc", MyProperty = "def" };
. They'll both have to be in the same assembly and in the same namespace.
精彩评论