Is possible to use generics in LINQ-to-SQL mapping? - c#

Is there a way to define the following structure in a DataContext/DBML file?
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<IPermission> Permissions { get; set; }
}
public class User : IPermissionHolder
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<Permission<User>> Permissions { get; set; }
}
public class Group : IPermissionHolder
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<Permission<Group>> Permissions { get; set; }
}
public interface IPermissionHolder
{
int Id { get; set; }
}
public interface IPermission
{
Entity Entity { get; set; }
IPermissionHolder Holder { get; }
}
public class Permission<T> : IPermission where T : class, IPermissionHolder
{
public IPermissionHolder Holder
{
get { return PermissionHolder; }
}
public T PermissionHolder { get; set; }
public Entity Entity { get; set; }
}
If it's not possible, can you seggest another structure that fits my need?
Right now my DB is using two different tables for the GroupPermissions and the UserPermissions.
I don't like to have a common table where i have to add a "type" column... with two different table i have a much more strict control on the DB side.
Thanks for any help
P.S.: i'm still with the Framework 3.5, otherwise i could remove the IPermissionHolder interface and use co-variance
P.S.S.: asked also here, but no answer :(
http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/04a03c68-79c0-4136-907c-f81440e78c45
EDIT:
i'm trying different things and i'm facing two main problems
1) I want to have a IEnumerable, but it will never works because i don't want only to get data, but also to push data and an object can not be covariant and contravariant at the same time.
So first of all i should choose: read or write.
2)Here the most difficult issue: how do i map TWO Association to a single property?
User:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "User_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "UserId")]
public EntitySet<Permission<User>> Permissions{ ... }
Group
[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Group_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "GroupId")]
public EntitySet<Permission<Group>> Permissions { ... }
Permission
[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "???", Storage = "holder", ThisKey = "HolderId", OtherKey = "Id", IsForeignKey = true)]
public T PermissionHolder { ... }
Maybe i should call the Asscoiation "Holder_Permission"?!?

I tried with many different approach. I can say that with LINQ-TO-SQL is not possible to have generic mapping.
I will try with the Linq-To-Entity.

Related

How to avoid retrieving relational objects within relational reference loop using Entity Framework?

I'm trying to correctly design the structure of an Api controller method with the following object as a return type:
var obj= new CustomObject
{
Id = a.Id,
stampleProperty= a.stampleProperty,
stampleProperty= a.stampleProperty2,
B= a.B,
};
The baseline scenario consists of two objects A and B that have a "Many to Many" relationship as described below:
public class A
{
public int AId { get; set; }
public string sampleProperty{ get; set; }
public string sampleProperty2{ get; set; }
public virtual ICollection B { get; set; }
}
public class B
{
public int BId { get; set; }
public string sampleProperty3{ get; set; }
public string sampleProperty4{ get; set; }
public int ComyId { get; set; }
public virtual ICollection A{ get; set; }
public virtual Comy Comy{ get; set; }
}
Note: I cannot change the structure of the Database. In addition, I seek the best possible way to retrieve relational B objects from A object, without B's virtual properties of A.
The code that I tried on the controller, although it uses the "LazyLoading" approach, returns embedded type A objects within each associated type B object.
var a = db.A.FirstOrDefault(a => a.stampleProperty== stampleProperty);
var obj= new CustomObject
{
Id = a.AId,
sampleProperty= a.sampleProperty,
sampleProp= a.sampleProp,
B = a.B,
};
Return:
{
"AId":
"sampleProperty":
"sampleProp":
"B":[{
"BId":
"sampleProperty3":
"sampleProperty4":
"ComyId":
"A":[ **REFERENCE LOOP** ]
"ComyId":
"Comy":{}
}]
}
Goal: B objects without Virtual properties of A.
Due to the fact that I am on the process of learning this framework I am looking for the right approach to use these tools by avoiding raw SQL queries and multiple requests.
I would suggest creating an additional custom object and mapping the fields (either manually or using a framework like AutoMapper).
Applied to your example it could look something like:
public class CustomObjectA
{
public int Id { get; set; }
public string stampleProperty { get; set; }
public string stampleProperty2 { get; set; }
public CusomObjectB[] B { get; set; }
}
public class CustomObjectB
{
public int BId { get; set; }
public string sampleProperty3{ get; set; }
public string sampleProperty4{ get; set; }
public int ComyId { get; set; }
}
And the usage would look like this:
var a = db.A.FirstOrDefault(a => a.stampleProperty== stampleProperty);
var obj= new CustomObjectA
{
Id = a.AId,
sampleProperty= a.sampleProperty,
sampleProp= a.sampleProp,
B = a.B.Select(b => new CustomObjectB
{
BId = b.BId,
sampleProperty3 = b.sampleProperty3
//etc...
}).ToArray()
};
It's not necessarily a good idea to return database entities straight from an API for these sorts of reasons (along with some others, such as you may not want third parties consuming your API to be able to see every property in the database).
A common term for this approach is the use of DTOs (Data Transfer Objects). Here's a tutorial from Microsoft where they discuss it further https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5.

Converting infinitely nested objects in .NET Core

EDIT: I originally worded this question very poorly, stating the problem was with JSON serialization. The problem actually happens when I'm converting from my base classes to my returned models using my custom mappings. I apologize for the confusion. :(
I'm using .NET Core 1.1.0, EF Core 1.1.0. I'm querying an interest and want to get its category from my DB. EF is querying the DB properly, no problems there. The issue is that the returned category has a collection with one interest, which has one parent category, which has a collection with one interest, etc. When I attempt to convert this from the base class to my return model, I'm getting a stack overflow because it's attempting to convert the infinite loop of objects. The only way I can get around this is to set that collection to null before I serialize the category.
Interest/category is an example, but this is happening with ALL of the entities I query. Some of them get very messy with the loops to set the relevant properties to null, such as posts/comments.
What is the best way to address this? Right now I'm using custom mappings that I wrote to convert between base classes and the returned models, but I'm open to using any other tools that may be helpful. (I know my custom mappings are the reason for the stack overflow, but surely there must be a more graceful way of handling this than setting everything to null before projecting from base class to model.)
Classes:
public class InterestCategory
{
public long Id { get; set; }
public string Name { get; set; }
public ICollection<Interest> Interests { get; set; }
}
public class Interest
{
public long Id { get; set; }
public string Name { get; set; }
public long InterestCategoryId { get; set; }
public InterestCategory InterestCategory { get; set; }
}
Models:
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public long? InterestCategoryId { get; set; }
}
Mapping functions:
public static InterestCategoryModel ToModel(this InterestCategory category)
{
var m = new InterestCategoryModel
{
Name = category.Name,
Description = category.Description
};
if (category.Interests != null)
m.Interests = category.Interests.Select(i => i.ToModel()).ToList();
return m;
}
public static InterestModel ToModel(this Interest interest)
{
var m = new InterestModel
{
Name = interest.Name,
Description = interest.Description
};
if (interest.InterestCategory != null)
m.InterestCategory = interest.InterestCategory.ToModel();
return m;
}
This is returned by the query. (Sorry, needed to censor some things.)
This is not .NET Core related! JSON.NET is doing the serialization.
To disable it globally, just add this during configuration in Startup
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}));
edit:
Is it an option to remove the circular references form the model and have 2 distinct pair of models, depending on whether you want to show categories or interests?
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
Note that each of the models has a nested class for it's child objects, but they have their back references removed, so there would be no infinite reference during deserialization?

delete many to many entity framework [duplicate]

I'm using POCOs in combination with EF4 and some entities are in many-to-many relationships, in my case objects of class User and objects of class PrivilegeGroup.
This is how class User looks like:
public class User
{
public int UserID { set; get; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public bool IsActive { get; set; }
public List<PrivilegeGroup> PrivilegeGroups { get; set; }
}
And this is how class PrivilegeGroup looks like:
public class PrivilegeGroup
{
public int PrivilegeGroupID { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
public List<HasPrivilege> HasPrivileges { get; set; }
}
I have extended ObjectContext class
as follows:
public class AdminMDSContext : ObjectContext
{
public AdminMDSContext(string connectionString)
: base(connectionString)
{
this.DefaultContainerName = "MDSUsers_Entities";
_users = CreateObjectSet<User>();
_privilegeGroups = CreateObjectSet<PrivilegeGroup>();
}
private ObjectSet<User> _users;
private ObjectSet<PrivilegeGroup> _privilegeGroups;
public ObjectSet<User> Users
{
get { return _users; }
}
public ObjectSet<PrivilegeGroup> PrivilegeGroups
{
get { return _privilegeGroups; }
set { _privilegeGroups = value; }
}
}
Querying and insertion of these entities are working fine, but deletion is making problem, i.e. I want to remove PrivilegeGroup from one User without db roundtrip, but I don't know how to do it.
Can anyone please help me?
Interesting question. Here is how u do it.
var user = new User { UserId = 1 };
var admin = new Privilege { PrivilegeId = 1 };
user.Privileges.Add(admin);
db.Users.Attach(user);
user.Privileges.Remove(admin);
db.SaveChanges();
There are total of 4 different approaches to solving the same problem. but i think from what u are telling me, this should suffice but if u need more info, you can ping me directly through mail

NHibernate 'Bags' Implementation in Entity Framework

I started using EF with Code First recently and have come upon this issue which has left me rather perplexed. I will appreciate any feedback on this topic which will help me in resolving the said issue.
Please consider the following sample....
public class SomeType
{
public SomeType()
{
Properties = new List<BaseProperty>();
}
public int PrimaryKey { get; set; }
public string Name { get; set; }
public List<BaseProperty> Properties { get; set; }
}
public abstract class BaseProperty
{
public int PrimaryKey { get; set; }
public string PropertyName { get; set; }
// FK set through Type Configuration File.
public SomeType ParentInstance { get; set; }
}
public class PropertyA : BaseProperty
{
// some unique properties.
}
public class PropertyB : BaseProperty
{
// some unique properties.
}
public class PropertyC : BaseProperty
{
// some unique properties.
}
public class PropertyD : BaseProperty
{
// some unique properties.
}
All of this works great with the appropriate type configuration classes which map to 2 tables (1 for 'SomeType' and the second for 'BaseProperty' along with the remaining derived entities through the use of a discriminator column).
Now, due to circumstances beyond my control, I am being forced to modify 'SomeType' to something like this....
public class SomeType
{
public SomeType()
{
PropertiesAB = new List<BaseProperty>();
PropertiesC = new List<PropertyC>();
PropertiesD = new List<PropertyD>();
}
public int PrimaryKey { get; set; }
public string Name { get; set; }
public List<BaseProperty> PropertiesAB { get; set; } // collection of PropertyA and PropertyB
public List<PropertyC> PropertiesC { get; set; } // collection of PropertyC
public List<PropertyD> PropertiesD { get; set; } // collection of PropertyD
}
This would be very fairly easy to do in NHibernate using bags but is there an equivalent implimentation for this in EF using Code First ? Any thoughts ?
I do not want to write my own implimentation of a Collection which will forward and manipulate all operations to be performed on these new lists to a master list which will be actually mapped to the database.
Please ignore any missing "virtual" modifiers or anything else in the above code since it is only meant to be a sample and is NOT actually what I am using.
Thank you for your replies.
Worse comes to Worse, you can do something like this:
public class SomeType
{
public SomeType()
{
Properties = new List<BaseProperty>();
}
public int PrimaryKey { get; set; }
public string Name { get; set; }
public List<BaseProperty> Properties { get; set; }
public List<BaseProperty> PropertiesAB
{
get
{
return Properties.Where(p=>p is PropertyA || p is PropertyB);
}
set
{
//Remove all the properties already in the Properties collection of
//the type A and B and then
Properties.AddRange(value)
}
}
//Same with rest of the properties
}
You can also make the Properties property internal if the class is being used outside the domain layer

How to change a class without re-coding?

I am creating a drivers license object in my project, that employees will all have one of their own linked to their unique clock number. I have a separate table in my database for the driving license but in the future more types of vehicles will need to be added, is there anyway to do this without re-coding?
the columns in my database are the same as the attributes for the class below
public class LicenseDTO
{
public int ClockNo { get; set; }
public bool CBalance { get; set; }
public bool MR16 { get; set; }
public bool OrderPicker { get; set; }
public bool Reach { get; set; }
public bool Pedestrian { get; set; }
public bool Lorry { get; set; }
public bool Sweeper { get; set; }
public bool Washer { get; set; }
}
EDIT
I have tried to create this the best I could but I feel like it's really long winded and can be done a more efficient way. Here's an updated version of my code.
public class LicenseDTO
{
public int ClockNo { get; set; }
public List<Common.VehicleTypeDTO> Vehicles { get; set; }
}
public class VehicleTypeDTO
{
public string VehicleType { get; set; }
public bool Allowed { get; set; }
}
private void btnClockCardIn_Click(object sender, EventArgs e)
{
Common.LicenseDTO License = new Common.LicenseDTO();
List<Common.VehicleTypeDTO> Vehicles = new List<Common.VehicleTypeDTO>();
Common.VehicleTypeDTO CBalance = new Common.VehicleTypeDTO();
Common.VehicleTypeDTO MR16 = new Common.VehicleTypeDTO();
License.Vehicles = Vehicles;
CBalance.VehicleType = "CBalance";
CBalance.Allowed = true;
MR16.VehicleType = "MR16";
MR16.Allowed = false;
License.Vehicles.Add(CBalance);
License.Vehicles.Add(MR16);
foreach (Common.VehicleTypeDTO Vehicle in License.Vehicles)
{
MessageBox.Show(Vehicle.VehicleType + " " + Vehicle.Allowed);
}
}
Why not to create a table with the types of vehicles? In the future you can access to your table and insert more types.
public class VehicleTypeDTO
{
public int Id{ get; set; }
public string Name{ get; set; }
}
public class LicenseDTO
{
public int Id { get; set; }
public List<VehicleTypeDTO> VehicleTypes { get; set; }
}
You should have made an entity LicenseDTO with the attributes ClockNo and CBalance alongside an array of the type Vehicle. which will be an interface. the interface Vehicle can define any common methods the vehicles have. and all future vehicles will have to implement the interface. that way you dont have to change any code. Your current code cannot be "changed" without editing. You could try to extend your LicenseDTO class with another entityclass which implements the above interface. but there isnt much more you can do without editing.
If you want maintainability use interfaces, repository patterns, abstract classes and dependency injection to start with.
Instead of having multiple bit columns in your database to indicate different types of vehicles, have a single VehicleType table. Then you can add as many different vehicle types as you like and use the VehicleTypeID to uniquely identify them. You can then add more and more vehicle types to the table without having to write more code.
VehicleType
VehicleTypeID int
VehicleTypeName varchar(50)
public class LicenseDTO
{
public int ClockNo { get; set; }
public int VehicleTypeID { get; set; }
}
If you want to have multiple types of vehicles against a single ClockNo then use a list of int:
public class LicenseDTO
{
public int ClockNo { get; set; }
public List<int> VehicleTypes { get; set; }
}
Alternately you could have a reference to the VehicleType objects instead of just the ID's.

Categories