How to get the ObjectContext container Class name? without instantiating it like this. ObjectContext context = ((IObjectContextAdapter)db).ObjectContext; ..(4 learning purposes)
I know it is the way to do it http://bit.ly/1t38IPd but i want to learn how to get the name.
Thank you.
I'm not sure if I understood you correctly but you can use this:
string className = variable.GetType().Name
I think this right.
Model.Context.cs in App_Code If you are generating t4. if you are not it should be in Model.Designer.cs row 47.
Related
I have a navigation plugin which I want to extend to allow support for context navigation. I've got everything I need except for the NavigationOptions instance. In my INavigateFromHereProvider I get called with an IDataContext so can create the NavigationOptions from this using the NavigationOptions.FromDataContext method. Great.
In my context action I don't get called with a IDataContext, I get given a ICSharpContextActionDataProvider during construction. I can get my IDeclaredElement from this to base my navigation decision on, but I'm not sure how to create my NavigationOptions instance. It feels like I should be using NavigationOptions.FromWindowContext but I seems to need a PopupWindowContextSource and I don't know where to get one of these from.
So where can I get a relevant PopupWindowContextSource to create my NavigationOptions from?
Ah, discovered the answer (well, an answer).
I can use this:
var popupWindowContextSource = solution.GetComponent<MainWindowPopupWindowContext>().Source;
I'm not sure if this is the most appropriate thing to use, but it seems to work.
You might be better off using NavigationOptions.FromDataContext. This will pull a window context from the IDataContext that you've already got. This should give you a popup window context that's appropriate to where you've been invoked from - anchored to the editor window, or the tree view, or whatever. Using the MainWindowPopupContext will give you a context that's based on the main Visual Studio window, which will likely work and be fine, but might not be the most relevant.
I'm attempting to create a generic WPF form or page, that when called, will load in data from a LINQ table. Here is the concept:
I have three tables in a LINQ DataContext that are identical (apart from the data within)
TypeID and Type are the columns
I would like to generically pass that data in those tables into my second form depending on which table the user selects (essentially so they can narrow down the list of objects of said Type.
I've seen some responses, (in particular the accepted answer to this one LINQ query with a generic table) that are very close to what I am looking for, but not quite. One issue I have with the above answer is that T must be a reference type.
I've done more searching and found some more answers like:
someClass<T> : <T> where T
But unfortunately these are further from my own context and I am unable to bridge the two concepts of what is happening. Below I have posted what I hope to do.
someDataContext db = new someDataContext();
private void pageLoader<T>(){
newPage n = new newPage(T) //This is where I was hoping I could pass the table(s) to the constructor.
}
And here is the constructor:
newPage(T){
listBox l = new listBox();
l.datasource = T;
}
Any assistance in any direction would be helpful (besides MSDN, please. I've been there and I'm still lost.)
Let start from the top. LINQ is merely an abbreviation for Language Integrated Query. It is interchangeable with Lambda. Different syntax but both accomplish the same task. Querying a collection or datasource. See http://msdn.microsoft.com/en-ca/library/bb397926.aspx
You are referring to the EntityFramework Code First approach of creating a database. LINQ is merely a way to access and manipulate the information within.
With that out of the way, what you are pointing out is a Generic Method and a Generic Class. T is simply a standard naming convention for a generic type. You could use any representation you like. If you are going to be passing in entities, you might use TEntity for example.
See http://www.dotnetperls.com/generic-method
http://www.dotnetperls.com/generic
When you see someClass where T, this is a constraint for type parameters.
And finally, what you have been waiting for...
https://codereview.stackexchange.com/questions/19037/entity-framework-generic-repository-pattern
The following should put you on the right path.
http://blog.gauffin.org/2013/01/repository-pattern-done-right/ <- This would be more of a better starting tutorial
Im using SelfTrackingChanges and on relationship end "MANY" I havent got CreateSourceQuery() method :/
Is there any way I can achieve that still using SelfTrackingChanges ?
thanks for any help
Only EntityCollection<TEntity> and EntityReference<TEntity> classes override the IRelatedEnd.CreateSourceQuery as they implement IRelatedEnd interface and they both found on the EntityObjects which means you don't have it on your STEs and POCOs. What are you really trying to accomplish?
I wonder if there is any .net library to create "dummy" instances of an object. In my particular scenario im dealing with classes with lots of fields, so manual creation is not practical.
It would not be that hard to code this. But maybe there is something out there.
Thanks
There is. Check out NBuilder.
var products = Builder<Product>.CreateListOfSize(100)
.WhereTheFirst(10)
.Have(x => x.QuantityInStock = Generate.RandomInt(1, 2000))
.List;
And it goes from there. Pretty cool stuff.
I'm considering using PostSharp for entity-to-DTO and DTO-to-entity mapper. To do that task manualy for about a 100 entities would be a maintenence nightmare. I've looked at AutoMapper on codeplex, but i think the overhead might be a serious problem in my case, besides i feel that PostSharp could give me some extra control over the mapping convention. If anyone can share any experiences with this king of problems, that would be great.
The direction i'm think in is something like this (please somebody tell me if this is not possible):
The aspect that i am planing to stick to a class would fill the next two methods with content:
EntityType EntityToDTO(DTOType DTO) {}
DTOType DTOToEntity(EntityType Entity) {}
The first method would return entity based on DTO, the second one would do the oposite. Inside the aspect i'm planing to loop through each property, create new target and asign the value of a property to the counterpart from target object. Is this possible to do at compiletime witout any runtime overhead?
If your DTOs field names match your entity field names, then I'd use Duck Typing
http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx
http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx
Your code would work like this
UserDTO user = DuckTyping.Cast<UserDTO>(userEntity);
Basically, the duck typing library will be mapping over the fields by matching the names. They use dynamically generated IL to archive this.
If that has the potential of being too slow, I'd probably try to get CodeSmith to generate the methods for me.
If it helps, there is a project called PostSharp4ET that basically implements support for POCO objects to Entity Framework 1. See http://www.codeplex.com/efcontrib.
Note that PostSharp is not very good at generating new code. It is good at mixing new code with existing one. If you need to generate code, I would recommend writing a C# code generator based on reflection, and compile the resulting code. Or use a tool like CodeSmith, as mentioned previously.