EF Code First Duplicate child relationships - entity-framework

I have a Person table that I want to be mapped to multiple foreign keys in a Usage table. Something like this:
public class Usage{
int Id;
int OwnerId;
int CreatedBy;
public virtual Person Owner;
public virtual Person Creator;
}
public class Person{
int Id;
string Name;
}
Now I would like to use Fluent API so I can reference the properties Owner and Creator within an instance of the Usage table. Everything I read says you have to create relationships between both classes (e.g. also map a virtual collection back to the Usage table in the Person table) but I don't want to do that. I don't need a two way relationship. I just want to say that OwnerId and CreatedBy maps to the Person table and load it in memory.
modelBuilder.Entity<Usage>()
.WithOptional(x=>x.Owner).ForeignKey(x=>x.OwnerId)
.WithRequired(x=>x.Creator).ForeignKey(x=>x.CreatedBy)
Is this possible to do? I have a ton of relationships to the Person table and I don't want clutter it up with a bunch of virtual collections that I'll never use.
Thanks!

Related

Hazelcast Complex Object Model in Cache

I am looking to put a complex model into Hazelcast to use it as the data tier of an application with MapStore implementations rendering the actual objects to the database. So for example, lets say we have the following noxiously common model where I have stripped out getters and setters for brevity:
class Customer {
public int id;
public String name;
public Address address;
}
class Address {
public int id;
public String street;
public string city;
public String state;
public String zip;
}
class InterestGroup {
public int id;
public String name;
public List<Customer> customers;
}
This is a model that I want to store in the database but I also want to map into Hazelcast. Furthermore lets say that I want customers to share addresses such that if the address changes for one, it will change for all customers with that address.
I can write MapStore classes to read this information out of the database and even give each object a primary key to use as a map key. What I am having trouble with is setting up navigation within the map between entities. Lets say I obtain a customer and want to navigate to the address of that customer and then get all customers that use that address.
If I load customers and addresses into a map, I dont want to embed all customers in an address nor do I want to embed the address in each customer. I want to navigate transparrently from the customer to the address. Is there a means by which I could do this in hazelcast without breaking the dynamics of a nested object but while allowing addresses to live in another map? The situation is similar for interest groups. If I embed all customers in an interest group then I am duplicating data all over especially if the customer is in several interest groups.
To accomplish this without duplication all over do I have to compromise the object structure of my entities?
Thanks in advance.
If you know how to build the address_key for the address Hazelcast map you can implement HazecastInstanceAware to your model classes and build some kind of "lazy fetch" using getters to retrieve the address. Does that make sense to you? :)

Database first - EF created objects ignoring the name of foreign keys

I have created a mssql database and then used these instructions to create the datamodel. I have noticed something weird and it is also something which doesn't allow me to work with the model without confusion.
One of my objects is Book. Book contains an id, name, isbn, .. and also references to a Person table, like the author, the editor and the publisher.
the created model contains
string name;
int id;
string isbn;
int authorid;
int editorid;
int publisherid;
person person1;
person person2;
person person3;
Is there a way to change the names of the persons at creation? Because otherwise it is hard to figure out which person is who and I will have to change a lot manually.

How to represent objects from multiple databases in the EF Mdel / Data Context class

I am working on an intranet app. My data context class has some references to table(s) in other databases.
For example, one of my classes is like this:
public class SomeClass
{
public int Id {get;set;}
public string Name {get;set;}
...
public int EnteredById {get;set;}
}
Here is EnteredById is in fact a foreign key to Employee table in another database (on the same server) and that table is not part of the Data Context class.
I know I cannot create a foreign key on this property and this is fine as long as I can keep it in this class/ table and provide a value for it by going to Employee table in another database and retrieving it from there based on User’s Windows User Name.
How do I handle this situation while Employee table is not part of my Data Context?

Different types of Users with simplemembership in ASP.NET EF code first

In my app I want to have two different types of users. This types differ not only in role but also in attributes. I am using simpleMembership for userManagement and I want to ask what's the best way or practise to create data model for my situation in EF using code first. I was thinking about creating two another tables UsersType1 and UsersType2 which will have one to one relationship with table userProfiles so in the model I would have:
public class UserType1{
attr1
attr2
....
public int userProfileID {get; set;}
public virtual UserProfile userProfile {get; set;}
}
And same way for table UserType2.
I want to ask if this is a good way to go and if so, how should I modify userProfiles table to be able to access UserType1 and UserType2 through userprofile in code.
Thank you.
Use "Roles" to group users of the same type. Then use a custom table with the roleid as key.
I guess you could use inheritance, so UserType1 would inherit from UserProfile.
public class UserType1 : UserProfile
{
attr1
attr2
}
and when you fetch the UserProfile from the database you should be able to cast it to a UserType1 to access the attributes.

EF Code First One-To-One with Join Table

I am trying to configure my model to an existing database, and am running into a problem. The previous developer modeled a one-to-one relationship using a join table. If I have the following classes and database structure below, how can I map this using code first?
public class Title {
public Property Property { get; set; }
}
public class Property {
public Title TitleInsurance { get; set; }
}
tbTitle
-TitleID = PK
tbPropertyToTitle
-TitleID - FK to tbTitle.TitleID
-PropertID - FK to tbProperty.PropertyID
tbProperty
-PropertyID = PK
Code in VB.Net here, but should be easy to translate. Mark primary keys with the Key data attribute. Entity Framework will automatically look for properties named Class + ID, i.e. tbTitleID to assign as primary keys, but since that isn't applicable here, we need the Key attribute.
Overridable properties denote Navigation Properties. In C#, this should be equivalent to Virtual properties. When this navigation property is accessed, Entity Framework will automatically look for valid foreign key relations, and populate the appropriate data.
For a one-to-one relationship, Entity Framework expects that your two tables share the same primary key, as shown by TitleID here.
Public Class tbTitle
<Key()>
Public Property TitleID As Integer
...
Public Overridable Property Property As tbProperty
End Class
Public Class tbProperty
<Key()>
Public Property TitleID As Integer
...
Public Overridable Property Title As tbTitle
End Class
Looking through the fluent API, I don't see any way to map one to one relations through a join table. You might be able to fake it by setting it up as a many to many but then you would need a bit of extra code to ensure that your relation collections only ever have one item in them.