Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:42:46+00:00 2026-05-22T23:42:46+00:00

I’m developing an MVC3 application with EntityFramework v4.1. According to most common MVC best

  • 0

I’m developing an MVC3 application with EntityFramework v4.1.
According to most common MVC best practices, i should separate my data access layer objects from my view-model objects. I should not use entity objects as view-models for a good project design and for some possible XSS like security reasons. But since it is quite hard to trace the common properties between an entity class and its view-model equivalent while programming, i decided to use common interfaces both entity and view-model classes will be derived from. Such as :

public interface ICountry
{   
    int CountryId { get; set; }
    string CountryCode { get; set; }
    string CountryName { get; set; }
    string CountryLocalName { get; set; }
    ILanguage DefaultLanguage { get; set; }
}

…

/// entity class in my data access layer
public class Country : ICountry
{
    [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CountryId { get; set; }

    [Required(AllowEmptyStrings=false), MaxLength(6)]
    public string CountryCode { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryName { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryLocalName { get; set; }

    public string APropertyOnlyForDataEntity_001 { get; set; }
    public string APropertyOnlyForDataEntity_002 { get; set; }


    // this "Language" type is from data entity classes, derived from ILanguage
    public virtual Language DefaultLanguage { get; set; }
}

…

/// View Model class in my MVC application
public class Country : ICountry
{
    [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CountryId { get; set; }

    [Required(AllowEmptyStrings=false), MaxLength(6)]
    public string CountryCode { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryName { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryLocalName { get; set; }

    public string AnotherPropertyOnlyForViewModel_001 { get; set; }
    public string AnotherPropertyOnlyForViewModel_002 { get; set; }

    // this "Language" type is from view-model classes, derived from the same ILanguage
    public virtual Language DefaultLanguage { get; set; }
}

The problem here is, i can not use ILanguage DefaultLanguage { get; set; } in the ICountry interface, it gives build error. Even if my both “Country” classes have different “Language” typed objects, which are derived from same ILanguage interface, it says “it does not have a matching return type”. It expects me to put “public virtual ILanguage DefaultLanguage { get; set; }” instead. But when i put it in that way, then my Database is not being created properly. Because, in my DataContext, i am using the Language and Country entities such as DbSet< Language > and DbSet< Country >…

What should i do ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:42:47+00:00Added an answer on May 22, 2026 at 11:42 pm

    I believe this is the problem you’re running into. There’s some explanation there on how to fix it.

    That said the Interface might be overkill. I prefer to map between database classes and view models by using AutoMapper. That will take everything between the two with the same name and handle it automatically, then you can add/remove extra stuff as needed. Much easier to set up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.