微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在OnModelCreating

如何解决如何在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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?