I have noticed some strange behavior when it comes to nullable foreign keys in my database. I have just started playing with the entity framework so I'm probably doing something wrong, but I cannot figure this one out.
Say I have the following two tables: (CountryID is a foreign key that is nullable)
When I create a new entity model I end up with this:
But the CountryID properties are set to Nullable (None) - instead of True. Obviously this is a really trivial example, but with a large database it would be difficult switch all of these manually. Is there any way to have the entity framework use a nullable int? for these foreign keys?
Related
I have a domain model that makes use of Guids for primary/foreign keys. Is there a way to instruct the ModelBuilder to explicitly create database tables with nvarchar storage for these properties without applying annotations on every existing primary and foreign key in my entity classes?
Use the Microsoft.EntityFrameworkCore.Storage.ValueConversion.GuidToStringConverter value converter.
var guidToStringConverter = new GuidToStringConverter();
modelBuilder.Entity<YourEntity>()
.Property(ye => ye.PropertyThatIsAGuid)
.HasConversion(guidToStringConverter);
I forced to work with database where tables haven't auto increment. And I can't alter it. I want to insert entity using Entity Framework. I create an object of this entity and manually set it's Id field (primay key) and then make Add and SaveChanges. But I see in log, that EF clear the value of DbParameter for Id field. Is there any solution for this?
You can add an annotation of fluent configuration to tell EF the keys are manual. See Entering keys manually with Entity Framework. You could also add a custom convention to handle globally: Convention for DatabaseGeneratedOption.None
it's my understanding that ef code first does not support one to one relationships with an explicit foreign key on the dependent side that is NOT also the primary key... it's also my understanding that in order for breeze navigation properties to work, there MUST be a foreign key on the dependent side... so my question then is, is it correct to assume then that there's no way to get a one to one relationship with an explicit foreign key that is not also the primary key to work in breeze? if so, are there any workarounds? if not, how would i need to set up the metadata? we actually programatically generate our metadata following the nodb sample... is there any way through code to set up this type of navigation property? assume that we will still have a foreign key on the dependent side, only that it will be ignored by EF... thank you
This is a very interesting question. I'm pretty certain the answer is "No".
Take a look at this example from "metadata by hand". It describes the navigation from the dependent Product to it's principal Category.
navigationProperties: {
category: {
entityTypeName: "Category",
associationName: "Product_Category",
foreignKeyNames: ["categoryID"]
},
Notice that it identifies the FK property categoryID but is silent about the property on the principal side that the FK value must match.
That silence speaks volumes. Clearly "it goes without saying" that the matching property on the principal is the principle entity's key.
EF has sound Entity Data Modeling reasons for imposing this constraint (darned if I can remember what they are). Apparently Breeze follows suit.
I tried setting up a simple one-to-many relation in Entity Frameworks designer.
The tables are Category (1) and Transaction (N). Here's what I did:
Add "association"
End1 = Category, multiplicity 1, navigation property=Transaction
End2 = Transaction, multiplicity Many, navigation property = Category
Building it gave me the error "No mapping specified". Ok, makes sense. So I added this mapping:
Category
Category.CategoryID = Transaction.CategoryID
But the mapping designer also automatically adds a mapping for the Transaction table, which I cannot figure out how to delete or how to setup:
Transaction
Transaction.TransactionID = ???
Leaving it empty seems most valid, but that gives me: Error 3024 "Must specify mapping for all key properties (TransactionID)"
And trying to set it to a fake int property just hoping it's a compiler bug. But that gives me errors 3002 and 3003.
I dont get what to do. Isnt Associations meant to be used this way?
I suggest creating (or importing from the database) an entity for Catagory and an entity for Transaction. Add scalar properties to each as needed. Next, right-click on your entity, click Table Mapping, and map your entity properties to the table fields. For example, for the Category entity, map CategoryID field to a CategoryID property. Do the same for the other entity. THEN create the association.
Note that associations linked by exposed foreign keys do not have any mappings.
BTW, you'll probably want to add navigation properties as well.
I am developing StudentApp in .NET 3.5 SP1 MVC Application.
I have two tables
Course
CourseID, course_Name
Students
studentID, student_Name, courseID(fk)
Now I made StudentApp.dbml which is having both table as entities.
As Foreign key will not be present in student entity,
I can not display courseID in student model, more over i can not generate add, edit, list views.
So tell me how to display courseID(fk) in student & i also want course name instead.
And also dropdownbox showing course name & storing courseID in edit view .
I'm pretty sure you have to load the foreign reference for each entity. Since I have no idea how you've constructed your API, I'll have to give you a pseudocode'ish example, but I think this is what you need to do.
List<Students> studList = [your_db_facade].SelectStudents() // Or however you retrieve your students
foreach (Students singleStudent in studList)
singleStudent.Context.CourseReference.Load() //CourseReference.Load() should be in the framework
Then you get the CourseID and name from the single student entity like
singleStudent.Course.CourseID
singleStudent.Course.course_Name
It could look slightly different for you, but I think the key to solving your problem is CourseReference.Load().
If your using LINQ-to-SQL and created a DBML file in Visual Studio then the foreign keys can be listed through the Course property in the Student object (automatically generated so since it is a one-to-many relationship from Student). Sort of like this:
var studentCourseIds =
from s in context.Students
select s.Course.CourseID;
Since your goal is to find the coursename then it is already accessible with Student.Course.course_Name.
Can you post your DBML? Also, DBML is used in LINQ to SQL (L2S) - EDMX is the mapping used in the ADO Entity Framework. Are you using LINQ to SQL or the Entity Framework (EF)?
No matter which one you are using - they both support Foreign Keys and you would get a property representing either side of the relationship - you don't need to do anything special (the Foreign Key must exist in the database, of course).
In EF, the foreign keys are called "navigtion properties" and they work a little differently to Foreign Keys in L2S. Nothing major, but updating them and "eager loading" are somewhat different.
Just drop the tables onto the map in the designer in Visual Studio (or generate using command line equivalents if you prefer).
Regarding Foreign Keys and Drop Down Lists (and other UI goodness) - I wrote a couple of blog entries on some approaches which might suit you. One part is located here and part two is located here.
if you create the correct relationship in your SQL server database, then when you add the tables to your DBML designer, the relationships will be copied across also and your code will link up automatically.