Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从UML模型生成代码到列表<;T>;_C#_Asp.net_Ef Code First_Uml - Fatal编程技术网

C# 从UML模型生成代码到列表<;T>;

C# 从UML模型生成代码到列表<;T>;,c#,asp.net,ef-code-first,uml,C#,Asp.net,Ef Code First,Uml,通过VisualStudio2012,我创建了一个UML类图。从类图中,我生成了C#类 我没有展示所有代码和图像,而是在以下位置创建了一篇帖子: 当从类图生成代码时,我注意到,当存在关系时,会生成IEnumerable类型 这是可以的,但是我想使用生成的代码进行代码优先实现。下面,我创建了上下文。当前,上下文类仅包含一个关系 modelBuilder.Entity<Owner>() .HasKey(o => o.ownerID) .HasMany<Client>(o

通过VisualStudio2012,我创建了一个UML类图。从类图中,我生成了C#类

我没有展示所有代码和图像,而是在以下位置创建了一篇帖子:

当从类图生成代码时,我注意到,当存在关系时,会生成IEnumerable类型

这是可以的,但是我想使用生成的代码进行代码优先实现。下面,我创建了上下文。当前,上下文类仅包含一个关系

modelBuilder.Entity<Owner>()
.HasKey(o => o.ownerID)
.HasMany<Client>(o => (ICollection<Client>)o.Clients); 
    public class Client
    {
        public virtual int clientID
        {
            get;
            set;
        }

        public virtual int ownerID
        {
            get;
            set;
        }
    }

public class Owner
{
    public virtual int ownerID
    {
        get;
        set;
    }

    /// <summary>
    /// The pets and owners
    /// </summary>
    public virtual IEnumerable<Client> Clients
    {
        get;
        set;
    }
}
modelBuilder.Entity()
.HasKey(o=>o.ownerID)
.HasMany(o=>(ICollection)o.Clients);
这将创建与所有者和客户机表的一对多关系。因为HasMany需要ICollection类型,所以我将IEnumerable转换为ICollection

我的问题是:当我从UML类图生成代码时,是否有一种方法可以将关系生成为ICollection类型而不是IEnumerable类型。我的UML类图出现在我的博客上。我在上面提供的博客链接

最后,我只想创建我的项目的类图,生成代码,创建上下文(将创建DB tables},并利用实体进行开发

因为类图以IEnumerable的形式生成关系,下面,当我转换到ICollection时,上下文起作用。它创建一对多关系。我只想在类图中创建关系,而不必担心生成代码后的关系

public class Context : DbContext
{
    public DbSet<Client> Clients { get; set; }
    public DbSet<Medication> Medications { get; set; }
    public DbSet<MedicalHistory> MedicalHistories { get; set; }
    public DbSet<FeedingSchedule> FeedingSchedules { get; set; }
    public DbSet<Vaccine> Vaccines { get; set; }
    public DbSet<Owner> Owners { get; set; }
    public DbSet<Walk> Walks { get; set; }
    public DbSet<WhoWalk> WhoWalks { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Owner>().Property(b => b.fname).IsRequired().HasMaxLength(150);
        modelBuilder.Entity<Owner>().Property(b => b.lname).IsRequired().HasMaxLength(150);

        modelBuilder.Entity<Owner>().HasKey(o => o.ownerID).HasMany<Client>(o => (ICollection<Client>)o.Clients); 

        base.OnModelCreating(modelBuilder);
    }

}
公共类上下文:DbContext
{
公共数据库集客户端{get;set;}
公共数据库集{get;set;}
公共数据库集医疗历史记录{get;set;}
公共数据库集FeedingSchedules{get;set;}
公共数据库集{get;set;}
公共数据库集所有者{get;set;}
公共数据库集{get;set;}
公共DbSet{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Property(b=>b.fname.IsRequired().HasMaxLength(150);
modelBuilder.Entity().Property(b=>b.lname).IsRequired().HasMaxLength(150);
modelBuilder.Entity().HasKey(o=>o.ownerID).HasMany(o=>(ICollection)o.Clients);
基于模型创建(modelBuilder);
}
}
所有代码都可以在我的博客上查看:

我在下面提供了一个由UML类图生成的C#代码示例。目前,创建了两个类,它们之间有一对多的关系

modelBuilder.Entity<Owner>()
.HasKey(o => o.ownerID)
.HasMany<Client>(o => (ICollection<Client>)o.Clients); 
    public class Client
    {
        public virtual int clientID
        {
            get;
            set;
        }

        public virtual int ownerID
        {
            get;
            set;
        }
    }

public class Owner
{
    public virtual int ownerID
    {
        get;
        set;
    }

    /// <summary>
    /// The pets and owners
    /// </summary>
    public virtual IEnumerable<Client> Clients
    {
        get;
        set;
    }
}
公共类客户端
{
公共虚拟int客户端ID
{
得到;
设置
}
公共虚拟int所有者ID
{
得到;
设置
}
}
公共类所有者
{
公共虚拟int所有者ID
{
得到;
设置
}
/// 
///宠物和主人
/// 
公共虚拟IEnumerable客户端
{
得到;
设置
}
}
我希望类图生成以下代码:

public class Owner
{
    public virtual int ownerID
    {
        get;
        set;
    }

    /// <summary>
    /// The pets and owners
    /// </summary>
    public virtual ICollection<Client> Clients
    {
        get;
        set;
    }
}
公共类所有者
{
公共虚拟int所有者ID
{
得到;
设置
}
/// 
///宠物和主人
/// 
公共虚拟ICollection客户端
{
得到;
设置
}
}
当然可以

只需将multiplity保留为1,并将属性类型更改为字面上的
ICollection

当然


只需将多重性保留为1,并将属性类型更改为字面上的
ICollection

我也有同样的问题,另一个stackoverflow帖子帮助了我:

1) 更新t4文件,如以下帖子:


2) 我在修改ICollection的情况下复制了这些文件,并在VS->Architecture->Configure Default Code Generation Settings上更新了UML类图上的t4引用

我也有同样的问题,另一个stackoverflow帖子帮了我:

1) 更新t4文件,如以下帖子:

2) 我在修改ICollection的情况下复制了这些文件,并在VS->Architecture->Configure Default Code Generation Settings上更新了UML类图上的t4引用