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 6719023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:02:36+00:00 2026-05-26T09:02:36+00:00

I have the following models: public class Device { [Key] public int ID {

  • 0

I have the following models:

public class Device
{
    [Key]
    public int ID { get; set; }

    public int? SimCardID{ get; set; }

    [ForeignKey("SimCardID")]
    public virtual SimCard { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [MaxLength(16)]
    public string Imei { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [MaxLength(20)]
    public string Name { get; set; }
}

public class SimCard
{
    [Key]
    public int ID { get; set; }

    [DataType(DataType.PhoneNumber)]
    [Required]
    [MaxLength(20)]
    public string PhoneNumber { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [MaxLength(40)]
    public string SerialNumber { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [MaxLength(20)]
    public string Provider { get; set; }
}

One Device (1) has at the most one (c) sim card. Relationship is therefore 1:c. How can I implement that in EF Code First?

  • 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-26T09:02:37+00:00Added an answer on May 26, 2026 at 9:02 am

    This relation cannot be mapped with EF and it even doesn’t work in the database.

    You say that SIM cart can exists without device. That is ok but you also say that Device can exist without SIM card and you still want referential integrity and one-to-one relation. What does it mean?

    It means that Device table must have:

    • FK column to SIM card (relation)
    • FK column must be nullable (device can exists without SIM card)
    • FK column must have unique index (sim card can be assigned to only one device)

    First problem is in the database. If you place unique index to nullable column you will get some surprised result – null is considered as value and only single record can have null assigned otherwise it will violate unique constraint => that means you can have only one device without sim card.

    Second problem is in the EF. EF doesn’t support unique keys so it doesn’t see that one-to-one relation (and it will not create index for you). The only possibility to use real one-to-one relation in EF is to place FK on device’s PK = device will have same PK value as a SIM but in this scenario you cannot have device without SIM card.

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

Sidebar

Related Questions

Say I have the following models: public class Item { public int Id{ get;
I have the following view models: public class Search { public int Id {
I have the following models: WeatherForecast: namespace TravelAssistant.Model.HelperModels.Weather public class WeatherForecast:EntityBase<int> { public IList<DayForecast>
I have the following types and classes: namespace MVC.Models public class Page { public
Let just say I have the following data models: public class Account { public
I have the following class: public class AuthContext : DbContext { public DbSet<Models.Permission> Permissions
I have the following models: User: public class User : IEntity, INamedType { public
I have the following models in my ASP.NET MVC 3 project: public class Task
I have following ViewModel: public class Bulletin1ViewModel { [Required] public String NumberDelegations { get;
I have the following models: class A { // ...some properties public B InnerField

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.