Hello There, Guest!
View New Posts  |  View Today's Posts
[C#] Entity Framework Mapping

  • 0 Vote(s) - 0 Average


06-09-2014, 11:51 PM #1
Lee Stevens
Senior Member
***
Posts: 345 Threads:54 Joined: Feb 2012 Reputation: 12

Entity Framework Mapping
Hi All,

I've Googled for a few hours and still can't figure it out.

I have two Classes:

Computer
Code:
public class EntComputer
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EntComputerId { get; set; }

        public int EntIPRangeId { get; set; }
        public virtual EntIPRange IPRange { get; set; }
}

IP Range
Code:
public class EntIPRange
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EntIPRangeId { get; set; }
        public string EntIPRangeName { get; set; }
        public bool EntIPRangeEnabled { get; set; }
        public string EntIPRangeStartIP { get; set; }
        public string EntIPRangeEndIP { get; set; }
        public int EntIPRangeTimeout { get; set; }
        public DateTime? EntIPRangeLastScan { get; set; }
}

I'm trying to map and IP Range to a Computer i've used this:

Code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<EntComputer>().HasRequired(IP => IP.IPRange);
}

But when i try and get the IPRange from the Computer Entity i get null.

I'm currently using the below to get the IPRange, until i can get a fix.
Code:
public EntIPRange IPRange {
            get {
                using (var context = new EntManagerDBContext())
                {
                    return context.IPRanges.Where(x => x.EntIPRangeId == EntIPRangeId).FirstOrDefault();
                }
           }
        }

Any ideas?

Thanks.
Lee.
This post was last modified: 06-10-2014, 01:45 AM by Lee Stevens.

06-10-2014, 04:17 PM #2
Null
Moderator
*****
Moderators
Posts: 130 Threads:23 Joined: Feb 2012 Reputation: 4

RE: Entity Framework Mapping
Well, aside from the fact that you do not need to use the fluent API in this case and that the attributes are ineffectual due to code over convention, the one-to-one relationship appears to be defined properly.

Could the problem be that you are not inserting values correctly? Please update your question to include your DbContext and any other relevant code portions that insert data into either table.

Here is a working example:

Code:
public class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer(new Seed());
        using (var context = new EntManagerDBContext())
        {
            var computer = context.Computers.First();
            var ipRange = computer.IPRange;
            Debugger.Break();
            Console.WriteLine(ipRange.EntIPRangeId);
        }
    }
}

public class EntComputer
{
    public int EntComputerId { get; set; }
    public int EntIPRangeId { get; set; }
    public virtual EntIPRange IPRange { get; set; }
}

public class EntIPRange
{
    public int EntIPRangeId { get; set; }
    public string EntIPRangeName { get; set; }
    public bool EntIPRangeEnabled { get; set; }
    public string EntIPRangeStartIP { get; set; }
    public string EntIPRangeEndIP { get; set; }
    public int EntIPRangeTimeout { get; set; }
    public DateTime? EntIPRangeLastScan { get; set; }
}


public class Seed : DropCreateDatabaseAlways<EntManagerDBContext>
{
    public override void InitializeDatabase(EntManagerDBContext context)
    {
        var ipRange = new EntIPRange
        {
            EntIPRangeName = "abc",
            EntIPRangeEnabled = true,
            EntIPRangeStartIP = "",
            EntIPRangeEndIP = "",
            EntIPRangeTimeout = 10,
            EntIPRangeLastScan = DateTime.Now.AddDays(-7)
        };

        EntComputer computer = new EntComputer
        {
            EntComputerId = 1,
            IPRange = ipRange
        };

        context.Computers.Add(computer);
        context.SaveChanges();
    }
}

public class EntManagerDBContext : DbContext
{
    public DbSet<EntComputer> Computers { get; set; }
}
This post was last modified: 06-10-2014, 04:21 PM by Null.

06-10-2014, 05:05 PM #3
Lee Stevens
Senior Member
***
Posts: 345 Threads:54 Joined: Feb 2012 Reputation: 12

RE: Entity Framework Mapping
Ok, so i must of miss-understood, i thought mapping the EntIPRangeId would allow me to pull the data instead of assigning it like you have?

06-10-2014, 05:27 PM #4
Null
Moderator
*****
Moderators
Posts: 130 Threads:23 Joined: Feb 2012 Reputation: 4

RE: Entity Framework Mapping
Entity Framework uses a design approach called code over convention which simply means that Entity Framework can deduce your intention without you having to explicitly configure constraints using the Fluent API. Here the EntIPRangeId property is mapped implicitly according to the conventions used.

The code that I listed both "pulls" and "assigns" data so I am not sure that I understand your latest question.

Originally you said that

Quote:But when i try and get the IPRange from the Computer Entity i get null.

This segment of code resolves the IPRange and proves that it is not null.

Code:
using (var context = new EntManagerDBContext())
{
    var computer = context.Computers.First();
    var ipRange = computer.IPRange;
    Debugger.Break();
    Console.WriteLine(ipRange.EntIPRangeId);
}
This post was last modified: 06-10-2014, 05:30 PM by Null.

06-10-2014, 06:25 PM #5
Lee Stevens
Senior Member
***
Posts: 345 Threads:54 Joined: Feb 2012 Reputation: 12

RE: Entity Framework Mapping
Yes because your assigning ipRange in the InitializeDatabase to the computer. The way i though it worked was you assign the IP Range Id in the Computer entity and it pulls from the IPRange DbSet instead of being assigned to the Computer.




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  witch framework use to make myapp work on w7 w8 W8.1 teck 5 2,597 05-18-2014, 09:20 PM
Last Post: AceInfinity
   Rx Framework AceInfinity 2 2,825 05-28-2013, 12:11 AM
Last Post: AceInfinity
   Entity Framework. _HAWK_ 7 3,668 05-26-2012, 04:34 PM
Last Post: _HAWK_
  Pro C# and the .NET 4.5 Framework - Apress AceInfinity 1 2,152 04-22-2012, 08:23 AM
Last Post: _HAWK_
  Check Your Currently Installed .Net Framework's AceInfinity 11 10,043 08-15-2011, 08:11 AM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)