实体框架代码第一个连接表复制插入数据(Fluent API)

时间:2015-03-19 13:37:01

标签: c# .net entity-framework code-first

我目前正在创建一个连接到Azure移动服务的Windows 8.1应用程序,并使用EF6 Code First创建数据库。

目前的表格为AccountUserBusinessProfilePictureBlob。创建配置文件时,会在Account表中插入一个条目,然后根据用户的选择,输入UserBusiness表。

实例化Account后,会在User模型中创建新的BusinessProfilePictureAccount模型。

我打算实现ProfilePicture的方法是在创建Account时设置默认的配置文件图片,因此我从表中选择默认的配置文件图片blob并在创建时使用它ProfilePicture内的Account模型。我遇到的问题是,当传入查询返回的Blob时,它似乎也将blob插入Blob表,从而复制了条目,我不知道为什么。 (希望下面的App.xaml.cs代码更加清晰)

我不确定为什么会发生这种情况。仍然掌握FluentAPI所以我的知识不是最好的,所以任何提示/指导,以及帮助,将非常感谢!感谢。


(抱歉有很多代码)

服务端

数据对象

public class Account : EntityData
{
    public string Username { get; set; }
    public string Password { get; set; }

    public User User { get; set; }
    public Business Business { get; set; }
    public ProfilePicture ProfilePicture { get; set; }
}

public class Business : EntityData
{
    public string Name { get; set; }
    public string ContactNo { get; set; }

    public Account Account { get; set; }
}

public class User : EntityData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Account Account { get; set; }
}

public class ProfilePicture : EntityData
{
    public Account Account { get; set; }
    public Blob Blob { get; set; }
}

public class Blob : EntityData
{
    public string ContainerName { get; set; }
    public string ResourceName { get; set; }
    public string SASQueryString { get; set; }
    public string ImageURI { get; set; }
}

实体配置(仅限其构造函数):

public AccountConfig()
{
    HasKey(account => account.Id);
    Property(account => account.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(account => account.Password).IsRequired();
    Property(account => account.Username).IsRequired();
}

public BusinessConfig()
{
    Property(business => business.ContactNo).IsRequired();
    Property(business => business.Name).IsRequired();

    HasRequired(business => business.Account)
        .WithOptional(account => account.Business)
        .WillCascadeOnDelete();
}

public UserConfig()
{
    Property(user => user.FirstName).IsRequired();
    Property(user => user.LastName).IsRequired();

    HasRequired(user => user.Account)
        .WithOptional(account => account.User)
        .WillCascadeOnDelete();
}

public ProfilePictureConfig()
{
    HasRequired(profile => profile.Account).
        WithRequiredDependent(account => account.ProfilePicture).
        WillCascadeOnDelete();
    HasRequired(profile => profile.Blob);
}

public BlobConfig()
{
    HasKey(blob => blob.Id);
    Property(blob => blob.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(blob => blob.ContainerName).IsRequired();
    Property(blob => blob.ImageURI).IsRequired();
    Property(blob => blob.ResourceName).IsRequired();
    Property(blob => blob.SASQueryString).IsRequired();
}

客户端

模型

public class Account
{
    public string Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public User User { get; set; }
    public Business Business { get; set; }
    public ProfilePicture ProfilePicture { get; set; }
}

public class Business
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string ContactNo { get; set; }
}

public class User
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class ProfilePicture
{
    public string Id { get; set; }

    public Account Account { get; set; }
    public Blob Blob { get; set; }
}

public class Blob
{
    public string Id { get; set; }
    public string ContainerName { get; set; }
    public string ResourceName { get; set; }
    public string SASQueryString { get; set; }
    public string ImageURI { get; set; }
}

App.xaml.cs

Blob blob1 = new Blob
        {
            ContainerName = "Container1",
            ImageURI = "Uri1",
            ResourceName = "DefaultProfilePic",
            SASQueryString = "SomethingGoesHere"
        };
        await BlobTable.InsertAsync(blob1);

        List<Blob> pictures = await BlobTable.Where(blob => blob.ContainerName == "Container1").ToListAsync();

        Account account = new Account
        {
            Username = "User123",
            User = new User
            {
                FirstName = "John",
                LastName = "Smithy"
            },
            ProfilePicture = new ProfilePicture
            {
                Blob = pictures.First()
            },
            Password = "Password123"
        };

        string result = await MobileService.InvokeApiAsync<Account, string>("customAccount", account);

0 个答案:

没有答案