We have a web application in Spanish, and we want to localize it to another language. I would like to query the original table i.e. countries if the visitor is a spanish spoken or countries_en if the visitor is a english spoken.
Is this possible in EF (we use EF 6) and LINQ? How? Because the LINQ query would switch tables dynamically checking UICulture.
Related
I am developing for an existing application which uses a SQL database that is used by two applications. One uses Entity Framework to connect to the database. The other uses LINQ-to-SQL. The SQL database is designed so that there are some tables showing many-to-many relationships between rows in two tables. Entity Framework seems not to import these tables, apparently because it has some object-oriented idea for how many-to-many relationships ought to be represented. So far, the Entity Framework application has not needed to know about those tables, but now it should. I don't know how that works, and I am concerned that even if I learn about Entity Framework's exciting new way to represent these relationships, that it won't cooperate nicely with the other application or the database which is designed to use the many-to-many table.
I.e., there is a table of Foos, and a table of Bars, and then a table with Foo and Bar Ids that lists which Foos relate to which Bars, and I don't want to stop using this relationship table, particularly because there is another LINQ application that heavily uses this relationship table.
Questions:
If I learn to use Entity Framework's many-to-many system, will it use and update the many-to-many table that the other application uses?
If not, what is a good way to get Entity Framework to not ignore the many-to-many relationship table, so I can write code to use the existing table?
Yes, Entity Framework will manage your many-to-many tables for you. Pure link tables (that only have two foreign key columns) in EF are represented as relationships as opposed to POCO objects. The way this is done is that you tell EF that there is a relationship between two of your objects and that table X is where this relationship is stored. As an example in EF 4.1. which is what I'm currently using this is done like so:
modelBuilder.Entity<Foo>() //Let me tell you about Foo...
.HasMany(f => f.Bars) //The property in the Foo class that links to Bar objects is Bars
.WithMany(b => b.Foos) //The property in the Bar class that links to Foo objects is Foos
.Map(m => {
m.MapLeftKey("FooID"); //Name of the foreign key column in the link table for Foo
m.MapRightKey("BarID"); //Name of the foreign key column in the link table for Bar
m.ToTable("FooBar"); //Name of the link table
});
You can then make changes to this table by linking/unlinking objects in your code. You pretty much do something like
myFoo.Bars.Add(myBar); //Add a row to the link table
myFoo.Bars.Remove(myBar) //Delete a row from the link table
For a full implementation you should google your version of EF.
In case of link tables that contain extra columns (for example a creation date) they are represented by a POCO just like all the other tables. If you're really paranoid about EF's ability to manage your link tables you can force it to go this route by adding a unique id column to your pure link tables, but I'd definitely advice against it.
Think of it this way: EF has been around for a while now and has achieved a certain degree of maturity. Combine this with the fact that many-to-many relationships are not exactly rare in databases. Do you really think the designers of EF haven't dealt with your case?
I am using Entity Framework with asp.net mvc3 razor. Now I have a table which represents Countries (like India, US) etc. And my requirement is I need to open a pop-up with the flags of all countries which I have in my database. And when user click on one flag I need to show that particular country details first and remaining as line by line in the webgrid(asp.net mvc3 razor)
So I prepared a list of "Countries" by getting all the countries from database. And I prepared another list OrderofCountries by adding Order(property) "1" to the which user had clicked. And from 2 to all the remaining countries. Now I need to join this list i.e "OrderofContries" with the remaining tables (from database as for the requirement). But Entity Framework raises an error:
Unable to create a constant value of type 'Slmg.BusinessObjects.CountriesBO'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
My idea here is by using order property I can sort the data so that I can get the required data.
Can we join our prepared list with the database table in Entity Framework? How to solve my issue. Can any one please help me to find the solution.
No you cannot join list in your application with database table unless you pull all data from that table to your application by calling ToList or AsEnumerable.
Does anyone have any recommendations with localization of core data? My application will have information that will sometimes be the same in both langauges, such as a person's photo, or different such as the person's biography.
From what I understand, it's possible to localize the field names, but what's the best course of action for field values?
If you want to localize string values, you should create an entity named something like LocalizableText with attributes locale and localizedText. Instead of using NSStrings for your attributes, you will instead have a relationship to LocalizableText.
So your Person entity would have a relationship named biography to entity LocalizableText. It would also have a read-only property for localizedBiography which would check for the appropriate localizedText to return based on the current locale of your user.
In my web app a user can assign muliple tags to products (like the tagging here on stackoverflow).
There are three tables: products, tags and products_tags to implement a many to many relationship.
My question is, how would you implement this with the Entity Framework (LINQ to SQL):
"Insert only a new tag in the tags table if it doesnt already exist there".
So before the insert i have to check first if a tag exists, whats the best way to accomplish this (best performance) ??
thanks for answers
Simple: The Tag should then be the user assigned key/PK of the entity/table.
If you have troubles synchronizing this with the database, I am sure there's something like (N)Hibernate's merge Method in EntityFramework.
I am new to Entity Framework, and ORM's for that mather.
In the project that I'm involed in we have a legacy database,
with all its keys as strings, case-insensitive.
We are converting to MSSQL and want to use EF as ORM,
but have run in to a problem.
Here is an example that illustrates our problem:
TableA has a primary string key,
TableB has a reference to this primary key.
In LINQ we write something like:
var result = from t in context.TableB select t.TableA;
foreach( var r in result )
Console.WriteLine( r.someFieldInTableA );
if TableA contains a primary key that reads "A", and TableB contains two rows that references TableA but with different cases in the referenceing field, "a" and "A".
In our project we want both of the rows to endup in the result, but only the one
with the matching case will end up there.
Using the SQL Profiler, I have noticed that both of the rows are selected.
Is there a way to tell Entity Framework that the keys are case insensitive?
Edit:We have now tested this with NHibernate and come to the conclution that NHibernate works with case-insensitive keys. So NHibernate might be a better choice for us.I am however still interested in finding out if there is any way to change the behaviour of Entity Framework.
Thanks for your answer!
Problem is that if we add that constraint to the database now,
the legacy application might stop working because of how it is built.
Best for us would be, if possible, to change the behavior of EF.
I'm guessing it is not possible, but I'm giving it a shot.
Regards,
Fredrik
edit: The reason why I added an answer to my own question was that I added this question before I was a registerd user, and when I had registred my account I couldn't add comments or edit my post. Now the accounts are merged.
I think you need to make the change to the schema in SQL Server, not in EF. This post's answer, on how to make a column case-sensitive, looks like it will do the trick: T-SQL: How do I create a unique key that is case sensitive?
I know this isn't a perfect solution, but in LINQ why not do the join yourself. EF doesn't work because the .Designer.cs file returns objA.Equals(objB) when doing the join. .Equals is case sensitive.
var result = from t1 in context.TableB
join t2 in context.TableA on t1.someFieldInTableB.ToUpper() equals t2.someFieldInTableA.ToUpper();
Hackish I know, but LINQ to Entities is still in its infancy and the object classes that are designed are designed for specific reasons that do not handle exceptional cases in a design such as this.
Another alternative is that you can create your own code generator using T4 templates. Since everything is a public partial class you can create a navigation property that actually does the case insensitive comparisson that you are looking for.
To answer your question truthfully though, there is no "out of the box" way to get EF to do a navigation using case insensitive searching.
I came up with a workaround that "stitches up" the string based association in memory after the context has retrieved the rows from the database (hint: making using of the context.[EntityTypeCollection].Local property. You can see my answer at https://stackoverflow.com/a/12557796/62278
I know this isn't a perfect solution, but in LINQ why not do the join yourself. EF
doesn't work because the .Designer.cs file returns objA.Equals(objB) when doing the >> join. .Equals is case sensitive.
Well, not if you override the Equals method
The generated domain classes in EF are partial no? So it's fairly easy to replace the default Equals implementation of these classes by your own implementations (which of course would render it case insensitive )
BTW : a technique dat dates back from .NET 1.0
With all this .NET 3.5/4.0, Linq and Lambda violence, people tend to forget about the basics
As an alternative to the Entity Framework, you can use LINQ to SQL, which works well with relations involving case sensitive collations. Although this ORM does not offer all the flexibility of EF or NHibernate, it can be sufficient in many cases.
I've recently posted a thread on the official Microsoft Entity Framework forum:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d4aa6880-31b3-4ff2-b7f5-e2694d76772e