ApplicationDbContext.EntityName()返回null

时间:2016-09-12 14:00:26

标签: asp.net-mvc entity-framework

为什么db.Countries()在以下场景中为空 - 1. CityController

private String readOpportunity(ServletInputStream in, String boundary, Logger logger) throws MultipartUploadException, IOException {

    logger.finer("Reading opportunity number");

    // Check for content disposition

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = in.readLine(buffer, 0, BUFFER_SIZE);
    String line = new String(buffer, 0, bytesRead);

    if (!line.startsWith("Content-Disposition: form-data"))
        throw new MultipartUploadException("Missing or invalid content-disposition");

    // Get the opportunuity number

    if (line.indexOf("name=\"opportunity\"") == -1)
        throw new MultipartUploadException("Missing opportunity number");

    // Skip the blank seperator line

    bytesRead = in.readLine(buffer, 0, BUFFER_SIZE);
    if (bytesRead != 2)
        throw new MultipartUploadException("Missing seperator line");

    **bytesRead = in.readLine(buffer, 0, BUFFER_SIZE);**
    String opportunity = new String(buffer, 0, bytesRead);
    if (opportunity.length() < 3)
        throw new MultipartUploadException("Missing opportunity number");

    // Read ending boundary

    bytesRead = in.readLine(buffer, 0, BUFFER_SIZE);
    line = new String(buffer, 0, bytesRead);

    if (!line.startsWith(boundary))
        throw new MultipartUploadException("Missing or invalid ending boundary");

    // Remove trailing new line

    opportunity = opportunity.substring(0, opportunity.length() - 2);
    return opportunity;
}
  1. ApplicationDbContext

    [Authorize]
    public class CityController : Controller
    {        
        private ApplicationDbContext db = new ApplicationDbContext("CP");
    
        // GET: City/Create
        public ActionResult Create()
        {
            ViewBag.CountryId = new SelectList(db.Countries.ToList(), "CountryId", "Name");
            return View();
        }
    
  2. 国家/地区定义为 -

    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
        {
        }
    
        internal IDbSet<Country> Countries { get; set; }
    
        ...
    }
    
  3. 城市定义为 -

    [Table("Country")]
    public class Country
    {
        #region Fields
        private ICollection<City> _cities;
        #endregion
    
        #region Scalar Properties
        public Guid CountryId { get; set; }
        public string Name { get; set; }
        public string CountryCode { get; set; }
        #endregion
    
        #region Navigation Properties
        public virtual ICollection<City> Cities
        {
            get { return _cities ?? (_cities = new List<City>()); }
            set { _cities = value; }
        }
        #endregion
     }
    
  4. 没有填充国家/地区表记录并将国家/地区返回为空的原因可能是什么?

1 个答案:

答案 0 :(得分:1)

在节省了几个小时之后,我注意到了国内属性的Access-modifier是内部的。我公开了,魔法发生了!虽然我对它的部分内容没有任何解释,但它有效。

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }
    //internal IDbSet<Country> Countries { get; set; }
    public IDbSet<Country> Countries { get; set; }
    public IDbSet<City> Cities { get; set; }

谢谢大家。