我应该在EF Core模型中使用自定义设置器吗?

时间:2017-11-19 14:50:28

标签: c# entity-framework asp.net-core entity-framework-core

我只是想知道我是否应该在EF Core模型中使用自定义设置器。考虑这个非常简单的例子:

using System;

namespace EFTest.Models
{
    public class Reservation
    {
        public int ReservationId { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public int ResourceId { get; set; }
        public int CustomerId { get; set; }

        public Customer Customer { get; set; }
        public Resource Resource { get; set; }
    }

    public class Resource
    {
        public int ResourceId { get; set; }
        public string Name { get; set; }
    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }
}

问题在于,当我必须保存模型并向其添加类的实例时,它会处理从所述添加的实例中获取外键,就像这样:

public void SaveReservation()
{
    var db = new Datebase();
    var reservation = new Reservation(){ Start = new DateTime().Now, End = new DateTime().Now.AddDays(7)};

    reservation.Resource = db.Resources.Find(2);
    reservation.Customer = db.Customers.Find(4);

    db.Reservations.Add(reservation);
    db.SaveChanges();
} 

但是如果我为customer设置了外键属性,但是然后添加了一个资源的实例,例如,它完全无法处理获取外键,如下所示:

public void SaveReservation()
{
    var db = new Datebase();
    var reservation = new Reservation(){ Start = new DateTime().Now, End = new DateTime().Now.AddDays(7)};

    reservation.Resource = db.Resources.Find(2);
    reservation.CustomerId = 4;

    db.Reservations.Add(reservation);
    db.SaveChanges();
}

导致sqlite异常19,'未能获得外键'或某些。

我能想到的唯一解决方案是使用自定义setter和getter来处理基于键设置属性,反之亦然,例如:

using System;

namespace EFTest.Models
{
    public class Reservation
    {
        public int ReservationId { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public int ResourceId 
        { 
            get
            {
                if(_Resource != null)
                {
                    return _Resource.ResourceId;
                }
                else
                {
                    return 0;
                }
            } 
            set
            {
                if(_Resource != null && !_Resource.ResourceId.Equals(value))
                {
                    _Resource = null;
                }
            } 
        }
        public int CustomerId
        { 
            get
            {
                if(_Customer != null)
                {
                    return _Customer.CustomerId;
                }
                else
                {
                    return 0;
                }
            } 
            set
            {
                if(_Customer != null && !_Customer.CustomerId.Equals(value))
                {
                    _Customer = null;
                }
            } 
        }

        public Customer Customer 
        { 
            get
            {
                return _Customer;
            } 
            set
            {
                _Customer = value;
                if(value != null)
                {
                    CustomerId = _Customer.CustomerId;
                }
                else
                {
                    CustomerId = 0;
                }
            } 
        }
        private Customer _Customer { get; set; }

        public Resource Resource
        { 
            get
            {
                return _Resource;
            } 
            set
            {
                _Resource = value;
                if(value != null)
                {
                    ResourceId = _Resource.ResourceId;
                }
                else
                {
                    ResourceId = 0;
                }
            } 
        }
        private Resource _Resource { get; set; }
    }

    public class Resource
    {
        public int ResourceId { get; set; }
        public string Name { get; set; }
    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }
}

但我完全不确定这是处理问题的好方法,有人可以提供一些有关它是否是处理它的好方法的见解?以及是否有更好的?

谢谢。

0 个答案:

没有答案