EF4 Code Only - Map Columns to Property Complex type - orm

I have a table like this:
Name
Tree
Iron
Clay
Added
I want to map it to a model like this:
Name
Resources
Tree
Iron
Clay
Added
In makes sense to map it like this, when working with it in my program, but doing it that way in the databse would just make it more complex ... not would not add any useful things.
Is it possible with EF4 Code ONly?

public class Sample
{
public int Id { get; set;} // primary key required
public string Name {get;set;}
public DateTime Added{get;set;}
}
public class Resource
{
// no Id defined here
public string Tree{get;set;}
public string Iron { get;set;}
public string Clay { get;set;}
}
public class SampleDB : DbContext
{
//public DbSet<Resource> Resources { get; set; } // should not be there
public DbSet<Sample> Samples { get; set; }
}

Related

How to map and reference entities from other data sources with NHibernate

I'm currently working on and ASP.NET MVC application in which I have a User entity like follows:
public class User
{
public virtual int Id { get; protected set; }
public virtual string Name { get; protected set; }
public virtual string Role { get; protected set; }
public virtual Location Location { get; protected set; }
}
Where location is just as straightforward:
public class Location
{
public virtual string Id { get; protected set; }
public virtual string Building { get; protected set; }
public virtual string City { get; protected set; }
public virtual string Region { get; protected set; }
}
My complication arises because I want to populate the User from Active Directory and not the database. Additionally, several classes persisted to the database reference a user as a property. I've got an ADUserRepository for retrieval, but I don't know how to integrate these Users into my object graph when the rest is managed by NHibernate.
Is there a way for NHibernate to persist just an id for a User without it being a foreign key to a Users table? Can I map it as a component to accomplish this? I've also looked at implementing IUserType to make the translation. That way it would map to a simple field and ADUserRepository could be put in the chain to resolve the stored Id. Or am I trying to hack something that's not really feasible? This is my first time around with NHibernate so I appreciate any insight or solutions you can give. Thanks.
Update
It appears my best solution on this will be to map the User with an IUserType and inject (preferably with StructureMap) a service for populating the object before its returned. Framed in that light there are a couple of questions here that deal with the topic mostly suggesting the need for a custom ByteCodeProvider. Will I still need to do this in order for IUserType to take a parameterized constructor or do the comments here: NHibernate.ByteCode.LinFu.dll For NHibernate 3.2 make a difference?
using a Usertype to convert user to id and back
public class SomeClass
{
public virtual string Id { get; protected set; }
public virtual User User { get; protected set; }
}
// in FluentMapping (you have to translate if you want to use mapping by code)
public SomeClassMap()
{
Map(x => x.User).Column("user_id").CustomType<UserType>();
}
public class UserType : IUserType
{
void NullSafeSet(...)
{
NHibernateUtil.Int32.NullSafeSet(cmd, ((User)value).Id, index);
}
void NullSafeGet(...)
{
int id = (int)NHibernateUtil.Int32.NullSafeGet(cmd, ((User)value).Id, index);
var userrepository = GetItFromSomeWhere();
return userrepository.FindById(id);
}
}

MVC3 - Extending a Class and Updating the SQL Table

I am using MVC3 and Entity Framework. I have a class called User with 20 different properties. I have already created a database and filled it with some data. I want to break out the Addresses property and make it it's own class.
namespace NameSpace.Domain.Entities
{
public class User
{
public int UserId { get; set; }
...
...
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
}
}
I want to break out both Addresses like so
namespace NameSpace.Domain.Entities
{
public class User
{
public int UserId { get; set; }
...
...
public Addresses Addresses { get; set; }
}
public class Addresses
{
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
}
}
HERE'S MY QUESTION:
Since I already have the data table filled with data, how can I update this in the Server Explorer?
Thanks ( if you need more info please let me know )
If you are using EF code first 4.3 you can use the concept of migrations to achive what you want.
You will need to do a code based manual migration since you change is a bit to advanced for the framework to figure it out itselfe.
Further reading: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

Building application with Entity framework dbContext API issues

I am developing a WCF Service application. It is going to be a part of large system. It provides some business logic and is based on Entity framework 4.1. I want to divide application code into 2 tiers (projects in VS, dll's): Service (which contains business logic) and DAL.
I have such database model in my project
ClassModel
classID : int, PK
classIdentity : string
teacherName : string
statisticInfo : int
isRegistered : bool
StudentModel
studentID : int, PK
studentIdentity : string
classID : int, FK
For this I am generating code using dbContext templates and I get:
public partial class ClassModel
{
public ClassModel()
{
this.Student = new HashSet<StudentModel>();
}
public int ClassID { get; set; }
public string ClassIdentity { get; set; }
public string TeacherName { get; set; }
public int StatisticInfo { get; set; }
public bool IsRegistered { get; set; }
public virtual ICollection<TerminalModel> Terminal { get; set; }
}
public partial class StudentModel
{
public int StudentID { get; set; }
public string StudentIdentity { get; set; }
public bool IsRegistered { get; set; }
public virtual ClassModel Class { get; set; }
}
I want to expose only some of this information through the service, so I have different model as a data contract:
[DataContract]
public class Clas{
[DataMember]
public string ClassIdentity {get;set;}
[DataMember]
public string TeacherName {get;set;}
[DataMember]
public string ClassMark {get;set;} //computed from statisticInfo
[DataMember]
public int NumberOfStudents {get;set;} //amount of students in this class
}
And my part of my ServiceContract:
[OperationContract]
public void RegsterClass(Clas clas); //(if given clas does not exists adds it and) sets its isRegistered column to True
[OperationContract]
public Clas GetClass(string classIdentity);
As you can see some fields are not present, others are being computed.
In such case I have some concerns about how should I built application properly. Could you write example code which implements the interface methods using everything I mentioned in the way that you think is proper?
Try using T4 templates
It is possible to use T4 templates to generate the dbContext classes, the data transfer objecs (more on that later), the interface as well as all the two methods you have there for each entity in your model: RegsterClass and GetClass. (this would translate to RegsterStudent, GetStudent, and so on for every entity)
Then you can use AutoMapper on NuGet to map from Clas to ClassModel.
I've implemented something similar. I don't pass any of my dbcontext based entities across the wire. I use Data transfer objects for each entity. So a Toyota entity, has a ToyotaDto that has the data annotations and is used for all the WCF CRUD operations. When "Getting" a toyotaDto, I map Toyota to ToyotaDto and return the Dto, when saving, I map the Dto to an entity, of course deleting is done by key, so no Dto necessary.
There are several(1) good(2) examples(3) online you can modify to suit, and if you want I can paste in some of the templates I'm using.

DbDataController in MVC4 UpdateEntity failing

I have a datamodel like
ModelA which contains a Collection.
ModelB contains a Collection as a backreference. That is failing because of cyclic references if I query with Include("ModelB"). Not good but I solved that via setting ModelB.List=null for each element.
The problem now is submitting a changed ModelA tree: I am adding ModelB-entities to ModelA.ModelB[]. Now the UpdateEntity function is complaining the it could not add elements of type ModelB which are declared static. The JSON deserializer is creating static arrays.
How is it possible with the combination of upshot/MVC4 to submit datamodels which are not completely flat? As it is not possible right now to create your own DTO objects where you might figure out something I am stuck now...
After investigating the error a bit better, I think the problem is the cyclic backreference:
The ModelA->ModelB->ModelA is breaking the storage of the data. "Could not add data of type ModelA to type ModelB".
As I mentioned the backreference was set to Null because the cyclic ref serialisation problem...
I hope the will be some easier way on doing more manually with DTO objects where I have mroe control.
Please see also: MVC 4, Upshot entities cyclic references for the beginning of the journey...
To solve the cyclic backreference, you can use the ignoreDataMember attribute:
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection<Delivery> Deliveries { get; set; }
}
public class Delivery
{
[Key]
public int DeliveryId { get; set; }
public string Description { get; set; }
public bool IsDelivered { get; set; }
[IgnoreDataMember]
public virtual Customer Customer { get; set; }
public virtual int CustomerId { get; set; }
}
I posted a working solution to your problem in a different question: https://stackoverflow.com/a/10010695/1226140

NHibernate: map multiple columns into a single collection

Suppose I have a table:
ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------
and the .NET classes
class A {
int id;
List<MyEmail> emails;
}
class MyEmail {
string email;
}
I suppose there's no way to map those (multiple) columns into a single collection in NHibernate, or is there? :)
It's come to a point that we'd rather not tinker with the database schema anymore so we can't do much with the database, if that helps.
I would suggest working with Interfaces so you could do something like this
interface IUser
{
int Id {get; set;}
IEnumerable<string> Emails {get;}
}
class MyUser : IUser
{
public int Id {get; set;}
public IEnumerable<string> Emails
{
get
{
return new [] { SomeEmail, SomeOtherEmail };
}
}
public string SomeEmail { get; set; }
public string SomeOtherEmail { get; set; }
}
Your application can expect an IUser and not care where we got the list of emails. You would map MyUser in NH, while the application does not (and should not) care about the actual implementation.
If it doesn't have to be a collection, but could be a custom type instead, say EmailAddresses which contains three properties:
public class EmailAddresses
{
public virtual string Home { get; set; }
public virtual string Work { get; set; }
public virtual string Other { get; set; }
}
You could use a component to map the three columns into the three properties of this object as a single property on the parent:
public class MyUser
{
...
public virtual EmailAddresses { get; set; }
}
You can map these in NHibernate using components or if you're using Fluent NHibernate with the ComponentMap<T> classmap (automapper can't do components).
There is a feature that's very close to what you want, <dynamic-component>
The documentation at http://nhibernate.info/doc/nh/en/index.html#components-dynamic should get you started.