Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am creating my models manually.

I have five tables in my database as follows :

1. Members
2. MemberTypeMasters
3. Payments
4. Relationships
5. StatusMasters

My Database looks like :

enter image description here

So far I have 6 files containing auto-implemented properties as below :

Models
 |-- DBCS.cs
 |-- Member.cs
 |-- MemberTypeMaster.cs
 |-- Payment.cs
 |-- Relationship.cs
 |-- StatusMaster.cs

Code in DBCS.cs

using System;
using System.Data.Entity;

namespace Munim_File_Sharing.Models
{
    public class DBCS : DbContext
    {
        public DbSet<Member> Members { get; set; }
        public DbSet<MemberTypeMaster> MemberTypeMasters { get; set; }
        public DbSet<Payment> Payments { get; set; }
        public DbSet<Relationship> RelationShips { get; set; }
        public DbSet<StatusMaster> StatusMasters { get; set; }
    }
}

Code in Member.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Munim_File_Sharing.Models
{
    public class Member
    {
        [Key]
        public int MemberID { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public string MobileNo { get; set; }
        public string FileName { get; set; }
        public Nullable<DateTime> MemberSince { get; set; }
        public int MemberTypeID { get; set; }

        public virtual MemberTypeMaster MemberTypeMaster { get; set; }
        public virtual ICollection<Payment> Payments { get; set; }
        public virtual ICollection<Relationship> RelationshipSenders { get; set; }
        public virtual ICollection<Relationship> RelationshipReceivers { get; set; }
    }
}

Code in MemberTypeMaster.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Munim_File_Sharing.Models
{
    public class MemberTypeMaster
    {
        [Key]
        public int MemberTypeID { get; set; }
        public string MemberType { get; set; }

        public ICollection<Member> Members { get; set; }
    }
}

Code in Payment.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Munim_File_Sharing.Models
{
    public class Payment
    {
        [Key]
        public int PaymentID { get; set; }
        public decimal Amount { get; set; }
        public DateTime DateOfReceipt { get; set; }
        public int MemberID { get; set; }

        public virtual Member Member { get; set; }
    }

}

Code in Relationship.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Munim_File_Sharing.Models
{
    public class Relationship
    {
        [Key]
        public int RelationshipID { get; set; }
        public int RequestSenderID { get; set; }
        public int RequestReceiverID { get; set; }
        public int StatusID { get; set; }

        public virtual Member RequestSender { get; set; }
        public virtual Member RequestReciever { get; set; }
        public virtual StatusMaster Status { get; set; }
    }
}

Code in StatusMaster.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Munim_File_Sharing.Models
{
    public class StatusMaster
    {
        [Key]
        public int StatusID { get; set; }
        public string Status { get; set; }

        public virtual ICollection<Relationship> Relationships { get; set; }
    }
}

Connection String in web.Config

<connectionStrings>
    <add name="DBCS" connectionString="server=.; database=FileSharingDB; Integrated Security = SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>

Is there any problem in my above classes?

Am I forgetting anything in Models?

Please suggest me as this is my first project in mvc.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The above classes are used by entity framework for persistance, don't confuse these with the Models in MVC. While its possible to use them directly in MVC, its considered bad practise to so tightly couple your presentation layer to your persistance layer.

Look at tools like AutoMapper that can help you map from the above classes to your actual View Models. Try and have one view model per page and populate them with properties that make sense to the presentation of the data, not its storage.

ie:

public class User
{
    [Key]
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class UserModel
    {
        public string Name { get; set; }
    }

Mapper.CreateMap<User,UsersModel>()
    .ForMember(dst => dst.Name, m => m.MapFrom(src => src.FirstName + " " + src.LastName))

and

var userModel = Mapper.Map<User, UserModel>(user);

Then use the userModel in your view.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.