Entity Framework and LLBLGen - entity-framework-4

I have a database, which has two tables.
The Order table, and the Item table. The relationship is m:1.
I am using LLBLGen as the mapping tool to generate the source file.
I can see that the ItemID from the Item table is displayed in the Order entity from the designer of LLBLGen, which is correct.
However, when I try to call this generated source codes, the ItemID is not listed as a property of my Order class. Actually all my foreign key is gone.
Could anyone teach me how to generate the foreign key as a property?
Thanks a lot

It's a setting. Go to:
Project properties -> Output Setting values Tab -> EmitForeignKeyFields, check the checkbox. Ok -> regenerate code.
It's false by default.
Ps: if you post your question on our own support forums, you might get answers more quickly ;)

Related

Updating entity framework model after changing the data type of column in database table

I am working with entity framework 6 with database 1st approach. I changed the datatype of my columns in the table and after updating my model I got the following error:
Error 1 Error 2019: Member Mapping specified is not valid. The type
'Edm.Int32[Nullable=False,DefaultValue=]' of member 'Warranty' in type
'pjModel.Bill' is not compatible with
'SqlServer.nvarchar[Nullable=False,DefaultValue=,MaxLength=50,Unicode=True,FixedLength=False]'
of member 'Warranty' in type 'pjModel.Store.Bills'.
what I understand that there is some compatibility issue between datatypes of framework and sql server.
Also when I opened the .edmx file with XML viewer, it clearly shows that changes are propagated in the model i.e data type has been updated, but in the diagram view when I right click the particular column it still shows the old datatype.
I can provide more information if required.
The issue has been resolved. I manually change the type from the diagram view and then it get mapped with the model class.
Same problem here, using EF6, after changing on a database table a field from int to bit.
It was not enought to delete property from model designer, and create it again, as the table mapping refered to the old type.
I guess this could be useful for similar cases. The only way to solve it (unless you don't mind deleting entity and updating model from database) is to edit your Entity Data Model as text (by default, DataModel.edmx), locate the entity as <Entity> node inside <Schema> node and find the desired property that indicates the old type. As an example:
<Property Name="MyProperty" Type="int" Nullable="false" />
Simply change Type attribute to the desired type, return to Visual Studio, save the edmx file, and rebuild.

Remove entries from a table that maintains a many to many relationship asp.net mvc entity framework

I am writing an application based on an existing database. I have two tables, a server table and a support table (people who support the specified server). These tables can have a many to many relationship, and as such I cannot maintain a foreign key within one of the tables pointing to another.
The solution that the person who designed the schema came up with was to add a third table, a server support junction, that has just two columns - ServerID and SupportID, both foreign keys pointing to their respective table.
When I import this database schema into Entity Framework, it gives me the following warning:
Warning 2 Error 6002: The
table/view 'dbo.Server_Support_Junction' does
not have a primary key defined. The key has been inferred and the definition
was created as a read-only table/view.
As such, the table does not appear in the edmx model and it does not create a class for the table.
As part of the application, I would like the DBA to be able to delete a server or a support (they leave the company/no longer support a certain server/etc). Is entity framework smart enough to see that this table is purely relational and will remove any connections when a support or server is deleted? Or must this be done explicitly?
If it must be done explicitly, what is a workaround for this? I tried adding a primary key called RelationID to the table, but it yelled at me saying that the primary key was not mapped or something.
Gert Arnold helped to find the solution. First, a primary key was added to the table consisting of both the Foreign keys, the SQL was:
ALTER TABLE dbo.Server_Support_Junction
ADD CONSTRAINT pk_ServerSupportJunc PRIMARY KEY (ServerID, SupportID)
I then updated the model by opening the edmx, right clicking on the blank space -> update model from database -> refresh -> finish.
To delete the relationship in the controller, the code was as follows:
Support support = db.Support.Find(id);
support.Servers.Clear();
db.Support.Remove(support);
db.SaveChanges();
Obviously you should do some error checking to make sure the entity was actually found, but that's the gist of it!
Special thanks to Gert Arnold!

How does Breeze handle database column defaults?

I can't find any info about this in the documentation, so I will ask here. How does breeze handle database column defaults? I have required columns in my database, but there are also default static values supplied for these in the database column definitions. Normally, I can insert null into these columns, and the new records will get the default. However, breeze doesn't seem to be aware of database column defaults, and the entities that have null in these columns fail validation on saving.
Thanks,
Mathias
Try editing the edmx xml by adding StoreGeneratedPattern = "Computed" attribute to the column with default value in the DB.
Edit:
Actually, before doing editing the xml, try setting the StoreGeneratedPattern property to Computed in the model editor itself.
Update:
This was fixed in Breeze 1.4.6 ( or later), available now.
Original Post:
There is currently in a bug in Breeze that should be fixed in the next release, out in about week. When this fix gets in then breeze will honor any defaultValues it finds in the EntityFramework data model.
One problem though is while it is easy to get 'defaultValues' into a Model First Entity Framework model via the properties editor, it's actually difficult to get it into a Code First EF model, unless you use fluent configuration. Unfortunately, EF ignores the [DefaultValue] attribute when constructing Code First model metadata.
One workaround that you can use now is to poke the 'defaultValue' directly onto any dataProperty. Something like:
var customerType = myEntityManager.metadataStore.getEntityType("Customer");
var fooProperty = customerType.getProperty("foo");
fooProperty.defaultValue = 123;

Facultative relation with Doctrine ORM

How should be implemented facultative one-to-one relation in Doctrine ORM and Symfony? Suppose there are some folders represented in database. Each folder can have a default icon or some custom icon represented in another table. How should this relation be described in the schema file? How can I tell that in case of given folder relation does or doesn't occur?
I myself have to guesses, but each seems to be not quite good:
1) Let's say I define folder_icon table with id column and folder_icon_id column in folder table and link these columns with foreign key. If folder_icon_id contains NULL, relation doesn't occur. If it contains some integer value it points to respective folder icon. When I implement it this way and try to obtain folder icon using something like $folder->getFolderIcon(), I get an instance of FolderIcon class with fields set to null (where I would rather excpect to get something like NULL, FALSE or Doctrine_Null). Why is it so? How should I check if the returned object is not 'real' folder icon?
2) Let's assume that I use method similar to previous but I define first row of folder_icon table to be the default icon, so that each folder that doesn't have any custom icon selected is related to this first row. In this case there is no problem with getting some dummy instances of FolderIcon class. But there is a problem if custom folder icon is removed form database, as there is no onDelete behaviour 'SET 1' to relate any folders using the deleted icon with the default icon.
How should this problem be solved? What is the proper way to define this kind of relation in schema file?
The problem is with the magic methods getVariable
Use $folder->folder_icon and to test for an existence of that relationship use isset(). Have a read of the doctrine website docs about testing for the existence of a relationship, I'm currently mobile so unable to link to it.

How to get the foreign key in Entity Framework?

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.

Resources