Using Linq2Sql relationships for linq only - linq-to-sql

I have two tables in Linq2Sql are a one-to-many relationship. Each have a reference to each other, either by a collection, or a single instance value.
I want to ensure my end users only use the collections for linq queries only. I don't want them to be able to add items to the collection and "SubmitChanges".
How do I register the collection with Linq2Sql as a collection but also tell it to never update (relationships only, not properties/fields) the database with the items in the collection?

Well, you can disable changetracking on your datacontext.
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.objecttrackingenabled.aspx
f ObjectTrackingEnabled is false, DeferredLoadingEnabled is ignored and treated as false. In this case, the DataContext is read-only.

Related

Where to put the calls to stored procedures which do not return an entity (Spring Data JPA)

I have the following Entity classes
Associate
Project
Allocation
Associate and Project are connected by an Allocation.
I have the standard structure of a spring boot application - the above entity classes, controllers, services, and repositories for each of the entities.
If I need to find/add/update/delete an Associate, I put the code in the Associate related #Repository and similarly for all the other entities.
I have a stored procedure call, that is a complex amalgamation of all the three tables (and returns columns in all thee tables), that logically doesn't come under any of the Entity classes (Its not simple like - all the Projects of an Associate, so that I can put it in Associate related classes)
So my question is - where do I put the call to the stored procedure?
Should I create a separate Entity class for the result type of the stored procedure and add its own controllers, services, repositories although I am pretty sure there is a better way of doing it.
I'd say it depends.
Having a separate repository for this the right approach if the result type and the algorithm implemented is really something separate from the original entities.
But in some cases it is really just one of the entities with some extra data in which case I'd put it in the repository of that entity.
In the first case note that nothing forces you to implement the repository using Spring Data. It is perfectly fine for such a case to have a manually implemented repository that is just normal bean with an EntityManager injected. This way it doesn't need an entity nor id type.
As Jen and others mentioned, if the procedure is returning the collection of data from one or more tables, in my project i kept this in one of the entity repository class where it is most suitable. I have also followed EntityManager approach like below in my service class
id = (Integer) entityManager.createNativeQuery("select get_next_global_id(?, ?)")
.setParameter(1, sequence)
.setParameter(2, status.toString())
.getSingleResult();

Unwanted Navigation property appearing in entity framework

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.

Using same type for LINQ-to-SQL mapping on different tables

I have a LINQ-to-SQL data context in which two tables exist with different names but identical structures. One table (called CallRecords) holds live/current data, and the other (CallRecordsArchive) holds older records - but with the same field names as the live one.
With the basic mapping LINQ to SQL creates two classes CallRecord and CallRecordsArchive - but since they are the same I'd like to avoid this if possible? That way I don't have to write two queries for each instance?
I did consider creating a JOIN view but with millions of rows in both tables it would be a performance nightmare.
The way I've dealt with this is to create an interface for the common aspects of both tables and have both of the generated classes from your data context implement that interface through the use of the a partial class definition. This way when you want to deal with the type as a single concept you can always refer to it as the interface.
try to use inherit for this issue
check this link for more details.
one more
I hope it is help you.

Linq2Sql: linking tables without relationships

I'm new to Linq2Sql and to learn it I'm trying to setup reports using Linq2Sql against the FogBugz tables. But, since the FogBugz tables don't have any true Foreign Key relationships, LinqToSql isn't finding the relationships and I can't figure out how to create them through the Designer. So, is there a way to do this manually? If I do it manually and then later decide to add another table, will everything I did be overwritten?
You can add relationships (aka Associations) in the SQL2LINQ designer by right clicking the white space in the designer, choose Add -> Association.
Choose your parent and child tables (classes), then link the properties on the tables accordingly using the grid that appears below the dropdowns for parent and child classes

Define a one-to-one relationship with LinqToSQL

I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into issues mapping a fairly important one-to-one relationship, so I suspect I am using the feature incorrectly for my database design.
Assume two tables, Category and CategoryDetail. Category contains the CategoryId (PK), ParentId and TemplateId. CategoryDetail contains the CategoryId (FK), LanguageId, Title and Description (in the appropriate language), with a combined PK of CategoryId and LanguageId.
If I drag-and-drop these tables into the LinqToSQL designer, the resultant object model has Category with a collection of CategoryDetail objects, which should never be the case. I'd like to be able to filter on LanguageId at the DataContext level, meaning that the whole Category is encapsulated within Category.CategoryDetail, not all language version encapsulated within Category.CategoryDetails.
This database worked fine on my old object library (an old-school custom BOL and DAL), but I fear that LinqToSQL would require this to change in order to give me the required result.
What is the best way to make this relationship (and language filtering) as seamless as possible?
You can view properties of the association. (Right click on the line representing the association and show properties.) The properties will tell you if it is a one-to-one or one-to-many relationship. This is reflected in code by having either a single entity association (one-to-one) or an entity set association (one-to-many).
I would have to assume cant be a true 1 to 1. Sounds like you have a PK of CatID and Lang ID on the Cat Details table. That would explain why its putting a collection. I could be wrong as you didnt mention the PK's of the CatDetails table
EDIT: A combined Pk of CatID and Lang ID makes that a 1:m relationship, and Linq to SQL is actually doing the correct thing. The only way it could possibly be a true 1:1 is if you had a lang ID on the cat table as well and that was part of the FK. I htink you may have to rethink what you want to do, or how you want to implement it.
I think LINQ to SQL models the database structure directly.
You have two tables so it creates 2 objects.
Have you had a look at LINQ to Entities this allows you to create another layer above the database strucure to make for more readable classes.
Since you don't have a 1:1 relationship the mapping alone will not provide the desired functionality. However it is easy to provide a method in the parent auto-generated class that does the job:
public partial class Category
{
public IEnumerable<CategoryDetail> GetDetailsByLanguage(string langID)
{
return this.CategoryDetails.Where(c => c.LangID == langID);
}
}