如何在OnModelCreating

问题描述

我有一个Dotnet Core API,其中将API,域逻辑和数据访问分离到了单独的项目中。以前所有内容都塞满了api项目。我的项目结构如下:

API-Web api项目 核心-定义整个解决方案中使用的所有接口 域-使用核心项目中定义的接口定义域类和逻辑 数据-与数据库通信并处理信息的数据访问层

在定义接口之前,一切正常,但是知道我收到CS0266错误。接口是在核心项目中定义的,因此对象之间的关系由接口表示。例如,我的游戏界面如下:

using System;

namespace SudokuCollective.Core.Interfaces.Models
{
    public interface IGame : IEntityBase
    {
        int UserId { get; set; }
        IUser User { get; set; }
        int SudokuMatrixId { get; set; }
        ISudokuMatrix SudokuMatrix { get; set; }
        int SudokuSolutionId { get; set; }
        ISudokuSolution SudokuSolution { get; set; }
        bool ContinueGame { get; set; }
        int AppId { get; set; }
        DateTime DateCreated { get; set; }
        DateTime DateCompleted { get; set; }
        bool IsSolved();
    }
}

我的用户界面如下:

using System;
using System.Collections.Generic;

namespace SudokuCollective.Core.Interfaces.Models
{
    public interface IUser : IEntityBase
    {
        string UserName { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string NickName { get; set; }
        string FullName { get; }
        string Email { get; set; }
        string Password { get; set; }
        bool IsActive { get; set; }
        DateTime DateCreated { get; set; }
        DateTime DateUpdated { get; set; }
        ICollection<IGame> Games { get; set; }
        ICollection<IUserRole> Roles { get; set; }
        ICollection<IUserApp> Apps { get; set; }
        void ActivateUser();
        void DeactiveUser();
        void UpdateRoles();
    }
}

如您所见,用户与游戏之间的关系由游戏界面中的IUser界面和用户界面中的IGame集合表示。这些曾经是类,但是由于接口是在核心项目中定义的,而类是在域项目中定义的,因此我更改了它们的接口。

但是,现在我在DBContext文件的OnModelCreating方法中遇到以下错误

Error   CS0266  Cannot implicitly convert type 'System.Collections.Generic.ICollection<SudokuCollective.Core.Interfaces.Models.IGame>' to 'System.Collections.Generic.IEnumerable<SudokuCollective.Domain.Models.Game>'. An explicit conversion exists (are you missing a cast?)

在这里建立关系:

            modelBuilder.Entity<Game>()
                .HasOne(game => game.User)
                .WithMany(user => user.Games)
                .HasForeignKey(game => game.UserId)
                .OnDelete(DeleteBehavior.Cascade);

如何将“ System.Collections.Generic.ICollection ”转换为“ System.Collections.Generic.IEnumerable ”?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...