实体框架不写入数据库

时间:2015-04-12 19:04:20

标签: c# entity-framework

我尝试接受JSON响应,反序列化并存储在localdb中。一切正常,但它没有将数据写入我的数据库。我似乎无法弄清楚为什么我没有收到任何错误。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using DataFetcher.DataFieldsInClasses;
using DataFetcher.EF_Models;
using Newtonsoft.Json;


namespace DataFetcher
{
    class Program
    {
        static void Main(string[] args)
        {
            var teamNames = new TeamsList();
            teamNames.teamList.Sort();

            for (int i = 0; i < 30; i++)
            {
                var cities = teamNames.teamList.ElementAt(i);
                //URL for each indivual team
                string URL = @"http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/" + cities + "/iphone/clubroster.json";
                WebRequest wrGETURL;
                wrGETURL = WebRequest.Create(URL);
                HttpWebResponse response = wrGETURL.GetResponse() as HttpWebResponse;

                StreamReader responseStream = new StreamReader(response.GetResponseStream());

                var result = responseStream.ReadToEnd();
                //var parsedInformation = JsonConvert.DeserializeObject<RootObject>(result);
                foreach (var forwardData in JsonConvert.DeserializeObject<RootObject>(result).forwards)
                {
                    //test output
                    Console.WriteLine(forwardData.id + " " + " " + forwardData.position + " " + forwardData.name + " " + forwardData.twitterURL + " " + forwardData.height + " " + forwardData.birthdate);

                    //write to database using EF
                    using (var _temp_Player = new DataFetcherDBEntities())
                    {
                        //var players = _temp_Player.Set<Player>();
                        _temp_Player.Players.Attach(new Player
                        {
                            player_id = forwardData.id,
                            name = forwardData.name,
                            age = forwardData.age,
                            birthdate = forwardData.birthdate,
                            birthplace = forwardData.birthplace,
                            weight = forwardData.weight,
                            height = forwardData.height,
                            number = forwardData.number,
                            position = forwardData.position,
                            imageUrl = forwardData.imageUrl,
                            twitterURL = forwardData.twitterURL,
                            twitterHandle = forwardData.twitterHandle
                        });
                        _temp_Player.SaveChanges();
                    }

                }

            }
            Console.ReadKey();

        }
    }
}

任何提示/想法?
P.S我还在学习和自学。

*编辑,我将localdb复制到&#34; ... DataFetcher \ bin \ Debug \ Databases&#34;我的新错误是&#34;&#34;违反PRIMARY KEY约束&#39; PK__tmp_ms_x__44DA120C6655986D&#39;。无法在对象&#39; dbo.Player&#39;中插入重复键。重复键值为(8473492)。\ r \ n语句已终止。&#34;}&#34; 这对我来说没有意义,因为每个玩家都有一个唯一的ID(player_id = forwardData.id)

1 个答案:

答案 0 :(得分:0)

您需要使用_temp_Player.Players.Add而不是Attach。

当您执行附加时,您需要为EF设置实体状态,以检测它是否为新记录,并在保存更改期间将其插入数据库。