I have an interesting problem and wanted so see if anyone else has seen this. I've created a MVC 2 site using Visual studio 2010 beta 2. I'm using linq to sql data model objects with data annotations.
In my data model objects I'm using [ScaffoldColumn(false)] attribute to exclude the foreign key ID's from rendering to the UI when I use the EditorForModel method. For some reason the UI is rendering the foreign key table name.
e.g. if the foreign key is AccountID, i see the "account".
I wonder if this is a bug in the editorformodel or if I need to use a different/additional data annotation attribute to instruct editorformodel to not render anything.
You Linq to SQL classes will have a property for the foreign key itself (AccountID) as well as an EntitySet property for the related records in the Accounts table. If you open up the auto-generated designer.cs file under your linq to sql dbml - you can view all the properties of each class. I think by default the templated helpers are only supposed to generate an editor for the first level of properties. There is a "deep-dive" option that will extend the generated editor to more levels of properties. Check out Brad Wilson's blog on the subject (near the end of the post).
In general, trying to throw your auto-generated Linq to SQL classes into your View or annotate them with attributes gets pretty hairy. It might be worth checking into strongly typed view models where you could specify only the properties you're interested in displaying in your view.
Related
Sorry. Couldn't think of a better title. Here is my dilemma. my sql tables below:
create table MainTable (UserId,somefields);
create table SecondaryTable(pkid, somefields, UserId,CreatedBy,UpdatedBy);
UserId,CreatedBy,UpdatedBy are foreign keys referencing MainTable(UserId).
Now as part of my linq queries, I routinely(wrongly) do things like
dbContext.SecondaryTable.Where(r=>r.MainTable.someOtherChainingHere)
The point is, I always assumed(wrongly) that Linq would automatically pick the best match among possible fields. Since UserId is the common field, I thought it would refer to it. But it is not. I seems to go by alphabetical order. Meaning, in Visual Studio Intellisense when I
dbContext.SecondaryTable.Where(r=>r.
I see MainTable,MainTable1,MainTable2 as my possible options.
How would I know the right one to pick among those three???? I apologize if I am not being more articulate.
How I've done in is by using partial classes to add "programmer friendly names" like this:
public partial class secondarytable
{
public maintable createdByUser{
get
{
return this._maintable1.Entity;
}
set
{
this._maintable1= value;
}
}
etcetera for the remaining references.
I've chosen not to do any manual changes to the DBML so I can always recreate it from scratch.
You just need to be carefull that when you change the DBML and the foreign key definition, that the 1,2,3 are still referencing the right table.
Good question btw. I am very curious to see better options!
I am using Entity Framework to create a Model, I am pretty new to the Entity Framework so bear with me.
I want my database to always be the end all be all of what gets generated, so I dont wnat to make modifications to the model itself. I want to amke all modifications to the database and just hit "Update Model From Database." This has been working swimmingly!
However If I have a one to one relationship between two tables, and I have a foreign key constraint set in the database, I get a navigation property in the child table that goes back to the parent table.
So if I want to access the parent from the child I can do child.parent.fieldName
That sounds great in theory but my issue arises when I need to serialize the object for JSON created by the entity Framework. I always get an error because it tries to serialize the parent object along with the child object. which usually has an invalid state at this point so.. A) it cant be serialized and B) I wouldn't want all that extra info anyway.
Am I misconfiguring the database in some way? is there a way to have the database specify that I only want Parent.Child Navigation properties in the model? and not Child.Parent?
Am I misconfiguring the database in some way? is there a way to have the database specify that I only want Parent.Child Navigation properties in the model? and not Child.Parent?
No. Database knows nothing about your intention to use some tool on top of it. If you don't want the navigation property you must change it in EDMX by deleting it but sometimes you want the property but you also want a serialization. In such case you must either modify strategy used to generate your classes (if you are not using T4 templates it will be quite hard) to use some special attributes dependent on used serialization API to mark some properties as not serialized.
The correct approach is not serializing entities but creating special data transfer objects filled from entities which will contain only properties/data you are going to serialize so you will not get into this problem.
Following on from this question here:Entity Framework Reverse Engineer using Power Tools - No Primary Keys
I've reverse engineered a database using Entity Framework Power Tools Beta 2.
This has created a large number of POCO's and a mappings folder with the entity mappings.
When I attempt to create a controller using the Add Controller Dialog I get the following message box come up:
Unable to retrieve metadata for cruise model. One or more Validation errors were detected during model generation:
\tSystem.Data.Entity.EmdEntityTypes:: cruise table has no key defined. Define the key for this EntityType.
This repeats many times for all the related tables to the original one for which I was trying to create a controller.
The Controller itself is never created and on pressing OK I'm returned to the Add Controller Dialog.
The model in question definitley has a primary key defined in it's mapping file:
// Primary Key
this.HasKey(t => t.cruise_ID);
What am I doing wrong?
How does the controller find the mapping classes?
I've been able to replicate the error your receive, and it's related to trying to create a controller for the Model itself, rather than for the entity type.
The MVC scaffolding is designed to target an entity, then the related context - are you sure you're selecting the right items in the drop down?
In This image:
Your model drop down (Mine shows A (MVCExamples.Models)) should refer to what I assume is your cruise entity.
Your Data Context should be the DbContext that gets created (Mine shows Entities (MVCExamples.Models))
ADD :
using System.ComponentModel.DataAnnotations;
and a primary key to db class is calling'
The documentation for inaport states you can just map lookup fields and it will work out what types they are.
I am mapping from CRM 4 to CRM 2011 (using the CRM Connectors), however all my lookups fail with
A lookup value was mapped to account.{field name} but no target entity name was supplied and no default is available.
I have to fall back to adding a custom field, checking if their is a lookup id in the field, and then making a lookup value as per the documentation of guid::entityname using expressions which is painful.
Is this feature working for anyone else? Do i need to set up a child-parent relationship? I only ever add a map for the entity I'm working on.
Inaport will try to work out what the correct entity reference is and default it. For example, if the lookup is the foreign key in a child table, the entity reference will default to the parent.
There are some circumstances where a lookup may reference multiple entity types, and Inaport cannot infer the correct type. For example, and activity "regarding" lookup may reference 12 different entity types.
It could do a better job when a custom lookup is only referencing a single entity type, and a change request has been put into the system.
As you noted, when Inaport does not correctly infer the entity type you can force it by appending "::entityname" to the GUID you are mapping to the lookup field. This is discussed in more detail in the help.
HTH
Regards
David Evans
I'm creating a .dbml file from a database. My "property" table has foreign keys to the "county" table and to a "propertysource" table.
When code is generated, Property.Source is defined as a PropertySource type, but Property.County is defined as an int, instead of a County type.
I'm afraid I don't have the experience with LINQ to SQL to understand what's happening here or why that is or how to fix it. I can't find anything obvious about my schema that might cause this. How can I get the automagically generated code to recognize the County property of the Property object is a County type?
When you click on the Property in the DBML file, you can modify it's Type using the Properties window. In the Properties window, there's a field for Type. You can select some of the standard Types from the dropdown, or enter in your own custom Type using it's full name, ie. "MyProject.Location.Country.CountryType", etc.
Be sure to rebuild your project before you try to access this in IntelliSense. Hope this helps!
When you open the dbml designer make sure you have a diagram that shows two arrows from the Property class: one arrow going to the County class, and another going to the PropertySource class. Also, you should have those three classes in your designer as well.
If you don't see the object model that way then there is probably something wrong with the way your database schema is set up.
I would check to make sure your foreign keys are set up correctly.