Facebook API模型的外键关系

时间:2017-11-21 15:01:04

标签: c# entity-framework facebook-graph-api asp.net-web-api foreign-keys

我有来自Facebook API的一些数据,我需要将它们存储在Azure SQL Db上。 我创建了模型,我试图设置外键来链接表,但我总是有一些错误。

我的模特:

public class FacebookDataUser
{
    [Key]
    [JsonProperty("id")]
    public string FacebookDataUserId { get; set; }
    public string name { get; set; }
    public string birthday { get; set; }
    public string email { get; set; }
    public virtual Hometown hometown { get; set; }
    public virtual Location location { get; set; }
    public virtual Events events { get; set; }
    public virtual Likes likes { get; set; }
    public virtual Age_Range age_range { get; set; }
    public string gender { get; set; }
}

public class Hometown
{
    [Key]
    [JsonProperty("id")]
    public string HometownId { get; set; }
    public string name { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Location
{
    [Key]
    [JsonProperty("id")]
    public string LocationId { get; set; }
    public string name { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Events
{
    [Key]
    public string EventsId { get; set; }
    public Datum[] data { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Datum
{
    [Key]
    public string DatumId { get; set; }
    public string description { get; set; }
    public string name { get; set; }
    public DateTime start_time { get; set; }
    public string PlaceId { get; set; }
    public Place Place { get; set; }
    public int attending_count { get; set; }
    public string type { get; set; }
    public string rsvp_status { get; set; }
    public DateTime end_time { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Place
{
    [Key]
    public string PlaceId { get; set; }
    public string name { get; set; }
    public string LocationEventId { get; set; }
    public LocationEvent location { get; set; }

    public string DatumId { get; set; }
    public Datum Datum { get; set; }
}

public class LocationEvent
{
    [Key]
    public string LocationEventId { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public float latitude { get; set; }
    public float longitude { get; set; }
    public string state { get; set; }
    public string street { get; set; }
    public string zip { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Likes
{
    // Doesn't have ID for Likes, but I need to have a Key in all classes.
    // If I don't have, I get an exception
    [Key]
    public string LikesId { get; set; }
    public Datum1[] data { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

public class Datum1
{
    [Key]
    public string Datum1Id { get; set; }
    public string category { get; set; }
    public string name { get; set; }
    public int fan_count { get; set; }
    public string website { get; set; }
    public string LocationId { get; set; }
    public LocationEvent location { get; set; }
    public string[] emails { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}


public class Age_Range
{
    // Doesn't have ID for Age_Range, but I need to have a Key in all classes.
    // If I don't have, I get an exception
    [Key]
    public string Age_RangeId { get; set; }
    public int min { get; set; }

    public string FacebookDataUserId { get; set; }
    public FacebookDataUser FacebookDataUser { get; set; }
}

我得到了这个例外:

  

无法确定类型'ApiGroma.Models.Age_Range'和'ApiGroma.Models.FacebookDataUser'之间关联的主要结束。必须使用关系流畅API或数据注释显式配置此关联的主要结尾。

如果我在[Required]上添加Age_Range,我会从Facebook API获得此例外:

"modelState": {
        "facebookDataUser.age_range.FacebookDataUser": [
            "The FacebookDataUser field is required."

因此,我尝试在Add方法中的方法[HttpPost]之前“手动”填写外键的值。

facebookDataUser.hometown.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.location.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.age_range.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.likes.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.events.FacebookDataUserId = facebookDataUser.FacebookDataUserId;

db.FacebookDataUsers.Add(facebookDataUser);

但我一直收到异常。 这样做的正确方法是什么?

我开始寻找解决方案,阅读微软博客和其他人已经有2天了,但我无法解决这个问题。

OBS:我在上下文类中创建数据库。

Database.SetInitializer<MobileServiceContext>(new CreateDatabaseIfNotExists<MobileServiceContext>());

2 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,在将数据插入与这些用户相关的其他表(外键)之前,必须让用户进入数据库。 Insert into table with foreign key

@EDIT:正如所承诺的,这里有一些代码。我建议您更新表格以接受public class FacebookDataUser { public string FacebookDataUserId { get; set; } // You already have the primary key you need public string name { get; set; } public string birthday { get; set; } public string email { get; set; } public virtual Hometown hometown { get; set; } public virtual Location location { get; set; } public virtual Events events { get; set; } public virtual Likes likes { get; set; } public virtual Age_Range age_range { get; set; } public string gender { get; set; } public void InsertUser(FacebookDataUser Data, Likes MoreData) { using (SqlConnection myCon = new SqlConnection("connection_string")) { using (SqlCommand query = new SqlCommand("INSERT INTO users_table (@ID, ...) VALUES (ID, ...)", myCon)) { query.Parameters.AddWithValue("@ID", Data.FacebookDataUserId); // add more parameters... try { myCon.Open(); query.ExecuteNonQuery(); } catch(Exception e) { throw e; } finally { myCon.Close(); } } using (SqlCommand query = new SqlCommand("INSERT INTO likes_table (..., @USERID) VALUES (..., USERID)", myCon)) { // add more parameters... query.Parameters.AddWithValue("@USERID", Data.FacebookDataUserId); // you won't get any exception related to the foreign key because this user is already in the parent table try { myCon.Open(); query.ExecuteNonQuery(); } catch (Exception e) { throw e; } finally { myCon.Close(); } } } } } 和其他人中的空值。

library(tidyverse)

df_wide %>% 
    gather(variable, value, -Month) %>% 
    mutate(location = sub('.*(Cabo|Acapulco).*', '\\1', variable), 
           variable = sub('_?(Cabo|Acapulco)_?', '', variable)) %>% 
    spread(variable, value)
#> # A tibble: 24 x 6
#>    Month location BED_BUGS    BU_PCT  LOS_AVG TOTAL_OCCUPIED
#>  * <dbl>    <chr>    <dbl>     <dbl>    <dbl>          <dbl>
#>  1     1 Acapulco        3 0.6260116 4.307667           6498
#>  2     1     Cabo        5 0.6470034 5.223000          19216
#>  3     2 Acapulco        0 0.6777457 4.247500           6566
#>  4     2     Cabo        3 0.6167027 5.893571          17095
#>  5     3 Acapulco        1 0.6348126 4.327742           6809
#>  6     3     Cabo        5 0.6372108 5.229677          19556
#>  7     4 Acapulco        6 0.6548170 4.220000           6797
#>  8     4     Cabo        4 0.6357912 5.356667          18883
#>  9     5 Acapulco        5 0.6409659 4.162903           6875
#> 10     5     Cabo        2 0.6449006 5.344194          19792
#> # ... with 14 more rows

我没有像我提到的那样运行查询以获取用户ID,因为您已经拥有它,只是组织运行方法的方式就足够了。

答案 1 :(得分:0)

因此,在对代码进行一些更改和比较后,我得到了它。

public class FacebookDataUser
    {
        [Key,JsonProperty("id")]
        public string FacebookDataUserId { get; set; }
        public string name { get; set; }
        public string birthday { get; set; }
        public string email { get; set; }
        public virtual Hometown Hometown { get; set; }
        public virtual Location Location { get; set; }
        public virtual Events Events { get; set; }
        public virtual Likes Likes { get; set; }
        public virtual Age_Range Age_Range { get; set; }
        public string gender { get; set; }
    }

    public class Hometown
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int HometownId { get; set; }
        public string id { get; set; }
        public string name { get; set; }

    }

    public class Location
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int LocationId { get; set; }
        public string id { get; set; }
        public string name { get; set; }

    }

    public class Events
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EventsId { get; set; }
        [JsonProperty("data")]
        public ICollection<EventData> EventDatas { get; set; }
    }

    public class EventData
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EventDataId { get; set; }
        public string description { get; set; }
        public string name { get; set; }
        [Column(TypeName = "datetime2")]
        public DateTime start_time { get; set; }
        public virtual Place Place { get; set; }
        public int attending_count { get; set; }     
        public string type { get; set; }
        public string rsvp_status { get; set; }
        [Column(TypeName = "datetime2")]
        public DateTime end_time { get; set; }
    }

    public class Place
    {
        [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int PlaceId { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        [JsonProperty("location")]
        public virtual LocationEvent LocationEvent { get; set; }
    }

    public class LocationEvent
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int LocationEventId { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public float latitude { get; set; }
        public float longitude { get; set; }
        public string state { get; set; }
        public string street { get; set; }
        public string zip { get; set; }
    }

    public class Likes
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int LikesId { get; set; }
        [JsonProperty("data")]
        public virtual ICollection<LikesData> LikesData { get; set; }
    }

    public class LikesData
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int LikesDataId { get; set; }
        public string id { get; set; }
        public string category { get; set; }
        public string name { get; set; }
        public int fan_count { get; set; }
        public string website { get; set; }
        [JsonProperty("location")]
        public virtual LocationEvent LocationEvent { get; set; }
        [JsonProperty("emails")]
        public virtual ICollection<string> emails { get; set; }
    }

    public class Age_Range
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Age_RangeId { get; set; }
        public int min { get; set; }
    }