Can I get a DbList from a string of the name of the entity? - asp.net

I am builing a ui query builder with Entity Framework. I was thinking of letting the user choose a base entity type from a drop down list of entities, then populate a grid view as test. If you select this entity, your basic result will be the contexts of this grid view. Later I will add the ability to 'Include' other entities and filter. But for now I can't even get the basics. I have a drop down of all entities. But I do not see how to get a generic list based on the name of the entity. I think I need to use reflection, but can't figure that out. I can get this easily if I hard code the entity type, but that does not solve my problem.
I have DbContext set up, using entity Framework 5, and want the results on a ASP .Net webforms page.

Related

Edit Symfony entity from the frontend

I have a Symfony app using multiple entities.
A third-party analytic tool plugs to my database to create reportings.
What I would like to achieve, is being able to update the Symfony entity from the frontend in order to add new fields to the database tables (in order to get the new fields showing up in the reporting tool).
Anyone has a idea on how to achieve that?
Thanks in advance.
If i understand correctly, you wan't to be able to add fields to your entity dynamicaly.
I don't know if this is doable and if so, it would probably be messy and unsecure.
What you can do however is using sub-entities with dynamic key => values fields.
You should have one main entity, an entity with the list of your dynamic fields, a many to many relation between your main entity and your fields entities and a third entity with the actual values from those fields.

Get the entity context in a Data Transformer

I have a problem concerning the usage of a DataTransformer.
Basically, I am developing a translation tool for my application whose goal is to be as generic as possible.
For that, I chose to follow that model : Database modeling for international and multilingual purposes
So, in different entities in my application, I have translatable attributes that simply are references to i18n elements. Then, this i18n ID is referenced in Translation table entries, that handle translation strings.
I succeed handling my translation interface, but I now have a problem with my forms : Indeed, I want some of my entities to be created/updated via forms. The problem is that I don't want the user to set a i18n ID for the translatable fields, of course, but a text, so that it can be handled by my application to either update or create the related translation in database.
I thought then that creating a DataTransformer could be a good idea, so that I can get the related translation string from the i18nID that is in my Entity entry (for that way, no problem). But my problem here is for the opposite way :
How can I deal with creating/updating i18n entries in my reverseTransform() method without knowing the entity values context?
Is there any way to get the previous entity values so that I could get the i18 ID that is stored originally in my entity? I understand that a Data Transformer is theorically totally independent from my forms and my entities, but I'm totally blocked about how to handle this case.
Indeed, when I save my entity with my translated string, I have no way to know the entity context in my reverseTransform() method, that would have permitted me to get the i18nID of the entity and to update it.
I just have the string that typed the user, but I can't do anything with that, because I can't know if it is an update or not since I don't have access to my entities.
Do you have any clue to do that? Is trying to use a DataTransformer to perform this a bad idea?
Thank you !

MVC3 Entity Framework using default membership relationships

I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
This has few preconditions:
ASP.NET tables must be in the same database as your own tables
Previous precondition means that you must either create your database and tables manually (without automatic code-first generation) or you must use some custom initializer which will add non mapped ASP.NET tables as part of database recreation
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example MembershipUser doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.

MVC 2 - Name Attributes on HTML Input Field when using Parent/Child Entities

I'm pretty new to MVC 2 using the Entity Framework. I have two tables Company {ID int identity PK,Name nvarchar} and User {ID int identity PK,UserName nvarchar,CompanyID int FK}. A Foreign Key exists between User and Company.
I generated my ADO.NET Entity Data Model, a Controller and a view to insert a record. My HTML form has the fields Company and UserName and the idea is when I click save a Company and User is inserted into the database. Sounds straight forward right!
My question is as follows:
I created a strongly-typed view derived from my 'User' entity. I'm using the the html helper Html.TextBoxFor(model => model.Organisation.Name) but the html name attribute for this input field is 'Organisation.Name'. My problem with this is that the dot throws up all sorts of issues in JQuery, which sees this as a property. If I want to change the name I read that I can use DataAnnotations but because I used the Entity Designer this involves using Buddy Classes. Seems like a bit of overkill just to change the html name attribute on this input field. Am I approaching this the right way or am I missing something here?
Thanks for the help !
I resolved this by taking a step back and reevaluating the way I was structuring my data. The end result was that my business entities were too closely coupled to my database schema and didn't reflect the domain I was working in. I redesigned my app. using POCO's to represent my business entities that better reflected my domain and this had the effect of 'flattening' the relational structure in this scenario, so instead of model.Organisation.Name I now have model.OrganisationName.

ASP.Net Entity Framework Model

Is it possible to add properties to my model that dont exist in the database?
For example I have a calendar table, I want to retireve this data in my MVC controller then work out time left until each entry starts and return this to the view. So I would like another property in my calendar model to hold time left which is a value that I will generate outside of the database.
I've tried just adding the property but when I do that I get errors because the property is not mapped to anything.
Is this even possible?
Thanks
You should be able to add the property to the Model but you will not be able to query it with LINQ. LINQ will ultimately build and expression which it will want to run against the database using SQL. Its at that point that your LINQ will fail to find a mapping from your property to a field somewhere.
If your query returns an IEnumerable of the actual type on which you have created the property your view may be able to access it. I can't remember if EF insists on mapping in that case, it may do.
You might find that you can create subsequent LINQ query that uses LINQ-to-objects if you want to provide some other composite type to your view.
It's a non-persistent property or transient. I don't know Entity Framwork well but with a quick google search you should find the answer.
BTW you can find a lot of tips here :
http://weblogs.asp.net/zeeshanhirani/archive/2008/12/18/my-christmas-present-to-the-entity-framework-community.aspx
After making a quick search myself and a test in VS2008 I don't see a way to exclude a property from the mapping. Maybe it requires you to edit manually the mapping file ? :(

Resources