我正在尝试使用组合键选择用户及其模块。我可以使用SQL查询在MySQL上进行选择,但是我是实体框架的新手,因此很难弄清楚。感谢您能获得的任何帮助。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int
main(int argc, char *argv[])
{
int s;
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;
const char *ifname = "can0";
if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Error while opening socket");
return -1;
}
strcpy(ifr.ifr_name, ifname);
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Error in socket bind");
return -2;
}
frame.can_id = 0x304;
frame.can_dlc = 2;
frame.data[0] = 0x11;
frame.data[1] = 0x22;
int sleep_ms = atoi(argv[1]) * 1000;
for (;;) {
nbytes = write(s, &frame, sizeof(struct can_frame));
if (nbytes == -1) {
perror("write");
return 1;
}
usleep(sleep_ms);
}
return 0;
}
这是我的数据库上下文类。
public class module
{
public int moduleid { get; set; }
public string modulename { get; set; }
[ForeignKey("module_moduleid")]
public ICollection<session> sessions { get; set; }
public ICollection<user_has_module> usermodule { get; set; }
}
public class user_has_module
{
public String userid { get; set; }
public int moduleid { get; set; }
public user user { get; set; }
public module module { get; set; }
}
public class user
{
[Key]
public string userid { get; set; }
public string userpw { get; set; }
public string fullname { get; set; }
public ICollection<user_has_module> usermodule { get; set; }
}
这是我要在实体框架核心中执行的查询。
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<user> user { get; set; }
public DbSet<module> module { get; set; }
public DbSet<user_has_module> user_has_module { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<user_has_module>()
.HasKey(x => new { x.userid, x.moduleid });
modelBuilder.Entity<user_has_module>()
.HasOne(x => x.user)
.WithMany(y => y.usermodule)
.HasForeignKey(y => y.userid);
modelBuilder.Entity<user_has_module>()
.HasOne(x => x.module)
.WithMany(y => y.usermodule)
.HasForeignKey(y => y.moduleid);
}
}
答案 0 :(得分:0)
要使用组合键从三个表中检索数据,您可能会受到以下示例的启发:
var query = from o in db.Orders
from p in db.Products
join d in db.OrderDetails
on new {o.OrderID, p.ProductID} equals new {d.OrderID, d.ProductID} into details
from d in details
select new {o.OrderID, p.ProductID, d.UnitPrice};
希望有帮助!