本文介绍了使用实体框架实时保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我构建了一个获取Twitter流的控制台应用程序,我可以毫无问题地看到实时推文。但是,当我向实体框架添加推文对象时,即使我看不到推文也没有任何反应。所以我想也许我不应该使用实体框架,也许那里
是另一种解决方案,我不知道因为我搜索了网络我找不到任何解决方案或建议。如何将实时推特数据添加到sql server? 非常感谢你。

类TwitterStream 
{

static void Main(string [] args)
{

//认证
Auth.SetUserCredentials("x","y","z","t");

//创建Stream
var stream = Stream.CreateFilteredStream();

//跟踪关键字
stream.AddTrack(" somethingPopuler");

//消息让你知道它正在运行
Console.WriteLine("我正在听Twitter");



//在制作跟踪器的推文产生时调用
stream.MatchingTweetReceived + =(sender,arguments)=>
{
var tweet = arguments.Tweet;
TweetSave(推文);
};

stream.StartStreamMatchingAllConditions();
}

private static void TweetSave(ITweet tweet)
{
Console.WriteLine(" tweet:");
using(var db = new Context())
{
var user = tweet.CreatedBy;
var fcount = 0;
var rcount = 0;
if(tweet.IsRetweet == false)
{
fcount = tweet.FavoriteCount;
rcount = tweet.RetweetCount;
}
其他
{
var rt = tweet.RetweetedTweet;
fcount = rt.RetweetCount;
rcount = rt.FavoriteCount;
}
var twit = new PTweet
{
created_at = tweet.CreatedAt.ToString(),
tid = tweet.Id,
text = tweet .FullText,
favorite_count = fcount,
retweet_count = rcount,
Uid = user.Id,
Uscreen_name = user.ScreenName,
Uname = user.Name,
Ulocation = user.Location,
Ustatuses_count = user.StatusesCount,
Uimage_url = user.ProfileImageUrlHttps

};
Console.WriteLine(twit.text);
db.Stream.Add(twit);
db.SaveChanges();
};

}
}



公共类上下文:DbContext 
{
public Context():base(" TwitterSon")
{}
public DbSet< PTweet>流{get;组; }
}


 app.config:

< connectionStrings>
< add name =" TwitterSon" connectionString =" server =(localdb)\ MSSQLLocalDB; database = SosyalJ; integrated security = true;"的providerName = QUOT; System.Data.SqlClient的" />
< / connectionStrings>

解决方案

Hello, I build a console application that get twitter stream and I can see live tweet without problem. But when I add tweet object to entity framework nothing happens even I can't see tweets.So I thought maybe I shouldn't use Entity framework and maybe there is another solution that I don't know cause I searched the web I ca't find any solution or advice. How can I add live twitter data to sql server ?  Thank you so much.

  class TwitterStream
    {

        static void Main(string[] args)
        {

            // Authentication
            Auth.SetUserCredentials("x","y","z","t");

            // Create the Stream
            var stream = Stream.CreateFilteredStream();

            // Keywords to Track
            stream.AddTrack("somethingPopuler");

            // Message so you know its running
            Console.WriteLine("I am listening to Twitter");



            // Called when a tweet maching the tracker is produced
            stream.MatchingTweetReceived += (sender, arguments) =>
            {
                var tweet = arguments.Tweet;
                TweetSave(tweet);
            };

            stream.StartStreamMatchingAllConditions();
        }

        private static void TweetSave(ITweet tweet)
        {
            Console.WriteLine(" tweet: ");
            using (var db = new Context())
            {
                var user = tweet.CreatedBy;
                var fcount=0;
                var rcount=0;
                if (tweet.IsRetweet == false)
                {
                    fcount = tweet.FavoriteCount;
                    rcount = tweet.RetweetCount;
                }
                else
                {
                    var rt = tweet.RetweetedTweet;
                    fcount = rt.RetweetCount;
                    rcount = rt.FavoriteCount;
                }
                var twit = new PTweet
                {
                    created_at = tweet.CreatedAt.ToString(),
                    tid = tweet.Id,
                    text = tweet.FullText,
                    favorite_count = fcount,
                    retweet_count = rcount,
                    Uid = user.Id,
                    Uscreen_name = user.ScreenName,
                    Uname = user.Name,
                    Ulocation = user.Location,
                    Ustatuses_count = user.StatusesCount,
                    Uimage_url = user.ProfileImageUrlHttps
                   
                };
                Console.WriteLine(twit.text);
                db.Stream.Add(twit);
                db.SaveChanges();
            };

        }
    }


  public class Context : DbContext
        {
            public Context() : base("TwitterSon")
            { }
            public DbSet<PTweet> Stream { get; set; }
        }
app.config :

<connectionStrings>
    <add name="TwitterSon" connectionString="server=(localdb)\MSSQLLocalDB;database=SosyalJ;integrated security=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
解决方案


这篇关于使用实体框架实时保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:13