C# 如何转换列表<;T>;到IEnumerable<;类别>;在OnModelCreating中
我有一个dotnetcoreapi,我将api、域逻辑和数据访问分离到不同的项目中。以前所有的东西都塞进了api项目中。我的项目结构如下:C# 如何转换列表<;T>;到IEnumerable<;类别>;在OnModelCreating中,c#,.net-core,fluent,C#,.net Core,Fluent,我有一个dotnetcoreapi,我将api、域逻辑和数据访问分离到不同的项目中。以前所有的东西都塞进了api项目中。我的项目结构如下: using System; namespace SudokuCollective.Core.Interfaces.Models { public interface IGame : IEntityBase { int UserId { get; set; } IUser User { get; set; }
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();
}
}
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();
}
}
我的用户界面如下:
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();
}
}
使用系统;
使用System.Collections.Generic;
命名空间SudokuCollective.Core.Interfaces.Models
{
公共接口IUser:IEntityBase
{
字符串用户名{get;set;}
字符串名{get;set;}
字符串LastName{get;set;}
字符串昵称{get;set;}
字符串全名{get;}
字符串电子邮件{get;set;}
字符串密码{get;set;}
布尔是活动的{get;set;}
DateTime DateCreated{get;set;}
DateTime DateUpdated{get;set;}
i集合游戏{get;set;}
ICollection角色{get;set;}
ICollection应用程序{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?)
错误CS0266无法将类型“System.Collections.Generic.ICollection”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)
这种关系在这里建立:
modelBuilder.Entity<Game>()
.HasOne(game => game.User)
.WithMany(user => user.Games)
.HasForeignKey(game => game.UserId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity()
.HasOne(game=>game.User)
.WithMany(user=>user.Games)
.HasForeignKey(game=>game.UserId)
.OnDelete(DeleteBehavior.Cascade);
如何将“System.Collections.Generic.ICollection”转换为“System.Collections.Generic.IEnumerable”?可以在此处查看该项目:分离数据对象和行为对象的替代方法,然后您可以摆脱核心项目(域项目将定义数据和行为)谢谢Fabio,看起来我必须这样做。我从github下载了示例项目,但它没有编译。SudokuCollective.Api.csproj似乎不见了。嘿,AntiqTech!是的,项目一团糟。我在一个api、一个保存所有定义的核心和一个保存数据和行为的数据项目中重构了该项目。它没有我想要的那么优雅,但它目前正在工作。请务必尝试上面列出的重构树。谢谢大家的评论!